Created
September 4, 2020 03:23
-
-
Save figueroadavid/87ca0d6b0d7860c9b6f175db51e5edde to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function New-ShortCut | |
| { | |
| <# | |
| .DESCRIPTION | |
| The script uses the ComObject WshShell to create a shortcut, | |
| When the shortcut is created, the ComObject is disposed of. | |
| .SYNOPSIS | |
| Creates a new shortcut | |
| .PARAMETER Target | |
| This is the target executable file of the shortcut. | |
| Most executable types (.exe, .com, .bat, .cmd, .vbs, etc.) can be the target. | |
| This must be a fully qualified path. | |
| .PARAMETER Arguments | |
| This is the argument string that is appended to the shortcut file target. | |
| The shortcut file automatically appends the argument string to the Target. | |
| .PARAMETER WorkingDirectory | |
| Sets the Working Directory path of the shortcut, this is the directory that the Target | |
| will be launched from. | |
| .PARAMETER LinkName | |
| This is the destination .lnk flie name. | |
| Just like the Target, this must be a fully qualified path. | |
| .PARAMETER WindowStyle | |
| This is one of the 3 "standard" window styles for the launched application. | |
| Default - this is the default location that the target executable uses | |
| Maximized - This launches the application in a maximized state | |
| Minimized - This launches the application minimized to the taskbar | |
| .PARAMETER IconPath | |
| This is the path to the file containing the icon. It can be a .ico file, a dll file, | |
| or an executable file that contains icons. By default, the shortcut will use the | |
| icon at the 0 index (the first icon available in the file). | |
| .PARAMETER IconIndex | |
| This is the index of the icon in the container file defined by IconPath | |
| .PARAMETER HotKeyString | |
| This defines the hotkey used for the shortcut. Each element of the hot key sequence needs | |
| to be defined and put together with '+' signs. | |
| As an example: | |
| 'Ctrl+Alt+1' | |
| 'Ctrl+Shift+6' | |
| .EXAMPLE | |
| New-ShortCut -Target C:\windows\system32\notepad.exe ` | |
| -Arguments c:\temp\newtext.txt ` | |
| -WorkingDirectory c:\temp ` | |
| -DestinationLinkName C:\temp\np2.lnk ` | |
| -WindowStyle Maximized ` | |
| -IconPath C:\windows\system32\shell32.dll ` | |
| -IconIndexNumber 5 ` | |
| -HotKeyString 'Ctrl+y' | |
| This creates a shortcut that: | |
| 1. Points to notepad.exe | |
| 2. Supplies an argument of c:\temp\newtext.txt (it would create it if didn't exist) | |
| 3. launches from c:\temp | |
| 4. is created as c:\temp\np.lnk | |
| 5. Notepad will launched maximized | |
| 6. It uses the 5th icon from shell32.dll (usually a diskette drive) | |
| 7. The file is saved as c:\temp\np.lnk | |
| 8. The shortcut has a hotkey of Ctrl+y | |
| #> | |
| [cmdletbinding()] | |
| Param | |
| ( | |
| [parameter(Mandatory)] | |
| [ValidateScript({ | |
| if (Test-Path -path $_ ) { | |
| if ($_ -eq (Get-Item -path $_).FullName) { | |
| $true | |
| } | |
| else { | |
| throw 'The supplied path is not a fully qualified path' | |
| } | |
| } | |
| else { | |
| throw 'The file does not exist' | |
| } | |
| })] | |
| [system.io.fileinfo]$Target, | |
| [parameter(ValueFromPipelineByPropertyName)] | |
| [string]$Arguments, | |
| [parameter(ValueFromPipelineByPropertyName)] | |
| [ValidateScript({ | |
| if ( (Test-Path -path $_) -and ( (Get-Item -path $_).PSIsContainer )) { | |
| $true | |
| } | |
| else { | |
| throw 'Either the directory does not exist, or the specified path is not a directory' | |
| } | |
| })] | |
| [string]$WorkingDirectory, | |
| [parameter(ValueFromPipelineByPropertyName)] | |
| [ValidateScript({ | |
| if ( $_ -eq ( (Resolve-Path -path $_).Path )) { | |
| if ($_.extension -eq '.lnk') { | |
| $true | |
| } | |
| else { | |
| throw 'The extension is not .lnk' | |
| } | |
| } | |
| else { | |
| throw 'The supplied path is not a fully qualified path' | |
| } | |
| })] | |
| [System.io.fileinfo]$DestinationLinkName = '{0}\temp.lnk' -f [environment]::GetFolderPath("desktop"), | |
| [parameter(ValueFromPipelineByPropertyName)] | |
| [ValidateSet('Default','Maximized','Minimized')] | |
| [string]$WindowStyle = 'Default', | |
| [parameter(ValueFromPipelineByPropertyName)] | |
| [ValidateScript({ Test-Path -path $_ })] | |
| [string]$IconPath, | |
| [parameter(ValueFromPipelineByPropertyName)] | |
| [ValidateScript({ $null -ne $IconPath })] | |
| [int]$IconIndexNumber, | |
| [parameter(ValueFromPipelineByPropertyName)] | |
| [ValidateScript({ | |
| if ($_ -match '(ctrl|alt|shift)+') { | |
| $true | |
| } | |
| else { | |
| throw 'The hotkey sequence does not contain one of more of Ctrl, Alt, Shift, or a + sign' | |
| } | |
| })] | |
| [string]$HotKeyString | |
| ) | |
| $wshShell = New-Object -ComObject WScript.Shell | |
| $WindowStyles = @{ | |
| Default = 1 | |
| Maximized = 3 | |
| Minimized = 7 | |
| } | |
| $shortcut = $wshShell.CreateShortcut( $DestinationLinkName ) | |
| $shortcut.TargetPath = $Target | |
| if ($arguments) { $shortcut.Arguments = $Arguments } | |
| if ($WorkingDirectory) { $shortcut.WorkingDirectory = $WorkingDirectory } | |
| if ($WindowStyle) { $shortcut.WindowStyle = $WindowStyles.$WindowStyle } | |
| if ($HotKeyString) { $shortcut.Hotkey = $HotKeyString} | |
| if ($IconPath) { | |
| if ($IconIndexNumber) { | |
| $shortcut.IconLocation = '{0},{1}' -f $IconPath,$IconIndexNumber | |
| } | |
| else { | |
| $shortcut.IconLocation = $IconPath | |
| } | |
| } | |
| try | |
| { | |
| $shortcut.Save() | |
| } | |
| catch | |
| { | |
| $_.Exception.Message | |
| } | |
| finally { | |
| $null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject($wshShell) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment