10-18-2010
Downloads: 363
File size: 1.5kB
Views: 2,828
Embed
 |
Set-Shortcut |
- function Get-Shortcut {
- param(
- $path = $null
- )
-
- $obj = New-Object -ComObject WScript.Shell
-
- if ($path -eq $null) {
- $pathUser = [System.Environment]::GetFolderPath('StartMenu')
- $pathCommon = $obj.SpecialFolders.Item('AllUsersStartMenu')
- $path = dir $pathUser, $pathCommon -Filter *.lnk -Recurse
- }
- $path | ForEach-Object {
- $link = $obj.CreateShortcut($_.FullName)
-
- $info = @{}
- $info.Hotkey = $link.Hotkey
- $info.TargetPath = $link.TargetPath
- $info.LinkPath = $link.FullName
- $info.Arguments = $link.Arguments
- $info.Target = try {Split-Path $info.TargetPath -Leaf } catch { 'n/a'}
- $info.Link = try { Split-Path $info.LinkPath -Leaf } catch { 'n/a'}
- $info.WindowStyle = $link.WindowStyle
- $info.IconLocation = $link.IconLocation
-
- New-Object PSObject -Property $info
- }
- }
-
- function Set-Shortcut {
- param(
- [Parameter(ValueFromPipelineByPropertyName=$true)]
- $LinkPath,
- $Hotkey,
- $IconLocation,
- $Arguments,
- $TargetPath
- )
- begin {
- $shell = New-Object -ComObject WScript.Shell
- }
-
- process {
- $link = $shell.CreateShortcut($LinkPath)
-
- $PSCmdlet.MyInvocation.BoundParameters.GetEnumerator() |
- Where-Object { $_.key -ne 'LinkPath' } |
- ForEach-Object { $link.$($_.key) = $_.value }
- $link.Save()
- }
- }
-
-
-
can change shortcut properties like assigning or removing keyboard shortcuts. In this demo, you can assign a new keyboard hotkey to your PowerShell shortcut (for this you need Admin privileges because the shortcut used is a shortcut for all users).