Set-Shortcut


posted by Tobias
10-18-2010

Downloads: 363
File size: 1.5kB
Views: 2,828

Embed
Set-Shortcut
  1. function Get-Shortcut
  2.     param
  3.         $path = $null 
  4.     
  5.      
  6.     $obj = New-Object -ComObject WScript.Shell 
  7.  
  8.     if ($path -eq $null) { 
  9.         $pathUser = [System.Environment]::GetFolderPath('StartMenu'
  10.         $pathCommon = $obj.SpecialFolders.Item('AllUsersStartMenu'
  11.         $path = dir $pathUser, $pathCommon -Filter *.lnk -Recurse  
  12.     
  13.     $path | ForEach-Object {  
  14.         $link = $obj.CreateShortcut($_.FullName
  15.  
  16.         $info = @{} 
  17.         $info.Hotkey = $link.Hotkey 
  18.         $info.TargetPath = $link.TargetPath 
  19.         $info.LinkPath = $link.FullName 
  20.         $info.Arguments = $link.Arguments 
  21.         $info.Target = try {Split-Path $info.TargetPath -Leaf } catch { 'n/a'
  22.         $info.Link = try { Split-Path $info.LinkPath -Leaf } catch { 'n/a'
  23.         $info.WindowStyle = $link.WindowStyle 
  24.         $info.IconLocation = $link.IconLocation 
  25.  
  26.         New-Object PSObject -Property $info 
  27.     
  28.  
  29. function Set-Shortcut
  30.     param
  31.     [Parameter(ValueFromPipelineByPropertyName=$true)] 
  32.     $LinkPath
  33.     $Hotkey
  34.     $IconLocation
  35.     $Arguments
  36.     $TargetPath 
  37.     
  38.     begin { 
  39.         $shell = New-Object -ComObject WScript.Shell 
  40.     
  41.      
  42.     process { 
  43.         $link = $shell.CreateShortcut($LinkPath
  44.  
  45.         $PSCmdlet.MyInvocation.BoundParameters.GetEnumerator() | 
  46.             Where-Object { $_.key -ne 'LinkPath' } | 
  47.             ForEach-Object { $link.$($_.key) = $_.value
  48.         $link.Save() 
  49.     
  50.  
  51. # assign F11 hotkey to PowerShell (uncomment line, make sure you have full admin privileges: 
  52. #Get-Shortcut | Where-Object { $_.LinkPath -like '*Windows PowerShell.lnk' } | Set-Shortcut -Hotkey F11 
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).
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.