Manage windows shortcuts (*.lnk, *.url) using Powershell (Wscript.shell)

Here is a little function that gets you object representation of a Windows shorcut given a path to the shortcut. Files can be piped to it from Get-childItem


function Get-SrvShortcut {
[CmdletBinding()]
Param
(

[parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Position=0,
HelpMessage=”Enter path to the shortcut”)]
[string] $FullName
)

begin
{
$WshShell = New-Object -ComObject WScript.Shell;
}

process
{
$WshShell.CreateShortcut($FullName)
}
}

The function can be then used in a script like this (this example modifies shortcuts that live in a certain folder (and subfolders) on many servers. It’s a real life scenario.

Get-ADComputer -filter {name -like "server*"} |
foreach {

$ServerName = $_.Name
$ServerName

$DestPath = "\\$($_.Name)\d`$\NetApplications\_shortcuts"

Get-ChildItem $DestPath -Recurse -Filter *.lnk| Get-SrvShortcut | where {$_.TargetPath -match "AppData" -or $_.WorkingDirectory -match "AppData"} |
foreach {
$NewTargetPath = $_.TargetPath -replace "AppData","NetApp"
$_.TargetPath
$NewTargetPath

$NewWorkingDirectory = $_.WorkingDirectory -replace "AppData","NetApp"
$_.WorkingDirectory
$NewWorkingDirectory

$_.TargetPath = $NewTargetPath
$_.WorkingDirectory = $NewWorkingDirectory
$_.Save()

}

}

Advertisements

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

smsagent

Tips, tricks and time-savers for the Windows and ConfigMgr administrator

To The Point

Anything about Technology and Business

Brian's Power Windows Blog

Microsoft in the Enterprise. Windows, Hyper-V, Exchange, SQL, and more!

PowerScripting Podcast

Shownotes and links for the PowerScripting Podcast, a podcast to help people learn Windows Powershell

Learn Powershell | Achieve More

What is this Powershell of which you speak?

%d bloggers like this: