Jump to content


0
[Tutorial]

Set-Shortcut (Powershell)



1 reply to this topic

#1 mashti

mashti

    Microsoft TE

  • Technical Expert
  • PipPipPipPipPipPip
  • 1,552 posts
  • 39194 thanks
  • LocationActive Directory Inside

Posted 21 March 2011 - 07:59 PM

Set-Shortcut.ps1

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()
	}
}

# assign F11 hotkey to PowerShell (uncomment line, make sure you have full admin privileges:
#Get-Shortcut | Where-Object { $_.LinkPath -like '*Windows PowerShell.lnk' } | Set-Shortcut -Hotkey F11


Filed under: Get-Shortcut, Shortcut, Set-Shortcut, $PSCmdlet
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).
Whenever death may surprise us, let it be welcome if our battle cry has reached even one receptive ear and another hand reaches out to take up our arms.
I know you are here to kill me. Shoot, coward, you are only going to kill a man.


Che Guevara

#2 fangbian

fangbian

    Junior Member

  • Members
  • PipPip
  • 5 posts
  • 3 thanks

Posted 12 February 2014 - 02:17 PM

View Postmashti, on 21 March 2011 - 07:59 PM, said:

Set-Shortcut.ps1

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()
	}
}

# assign F11 hotkey to PowerShell (uncomment line, make sure you have full admin privileges:
#Get-Shortcut | Where-Object { $_.LinkPath -like '*Windows PowerShell.lnk' } | Set-Shortcut -Hotkey F11


Filed under: Get-Shortcut, Shortcut, Set-Shortcut, $PSCmdlet
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).
Thank you very much for sharing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Organization

Community

Downloads

Test Providers

Site Info


Go to top