Powershell Naming shortcut - powershell

$wshshell = New-Object -ComObject WScript.Shell
$desktop = [System.Environment]::GetFolderPath('Desktop')
$lnk = $wshshell.CreateShortcut($sourceDir+"\ShortcutName.lnk")
$lnk.TargetPath = "$destination"
$lnk.Save()
I have a script in PowerShell that at the very end creates a shortcut in the $sourceDir directory which is currently being named "ShortcutName.lnk". I need to pass a variable which will be used as the name I've tried creating one called $test which is equal to the $item.FullName although I can't seem to append that to the CreateShortcut param $sourceDir+"\ShortcutName.lnk".

$lnk = $wshshell.CreateShortcut((Join-Path $sourceDir "ShortcutName.lnk"))
Should work. Join-Path will take care of adding slashes as needed.
If you want to use your existing file object:
$lnk = $wshshell.CreateShortcut((Join-Path $sourceDir ([io.path]::ChangeExtension($item.name, "lnk"))))

Related

Powershell creating shortcut

I have a powershell script that won't update the $TargetFile Properly. Here is my code:
$TargetFile = "C:\Users\wjschan\AppData\Roaming\DAS Toolbox\DAS Toolbox.exe"
$ShortcutFile = "$StartMenuPath\$ApplicationName.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$NewTarget = $TargetFile
$Shortcut.TargetPath = $TargetFile
$Shortcut.IconLocation = $IconLocation
$Shortcut.Save()
But when I right click on the shortcut it lists "This PC" as the target. However if I put this code in, it works fine:
$ShortcutFile = "$StartMenuPath\$ApplicationName.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$NewTarget = $TargetFile
$Shortcut.TargetPath = "C:\Users\wjschan\AppData\Roaming\DAS Toolbox\DAS Toolbox.exe"
$Shortcut.IconLocation = $IconLocation
$Shortcut.Save()
I'm at a loss as to why hard coding the file path works but a variable does not. It's worked before if $TargetFile points to a differnt path (Say a network share).
Win10, Build 1607 (Current Business Branch) - and no I can't get a newer build.
Apparently your $TargetFile is not a String type.
I suspect it is of a IO.FileInfo type as you probably dynamically building up the path.
To find out what object type it actually concerns, try: $TargetFile.PSTypeNames
To force it to a String, wrap it into quotes, like:
$Shortcut.TargetPath = "$TargetFile"
Or use: $TargetFile.ToString() or [String]$TargetFile

Create a lnk in PowerShell/batch [duplicate]

I am trying to create a short cut on public desktop for users, but this target path for this short cut is causing some issues.
$wshshell = New-Object -ComObject WScript.shell
$desktop = [System.Environment]::GetFolderPath("desktop")
$lnk = $wshshell.CreateShortcut("$desktop\CLMCPDEDEV.lnk")
$lnk.TargetPath = "C:\Program Files (x86)\Unisys\WebEnabler\Web Enabler.exe" "configfile=c:\development-installs\web-enabler-config\CLMCPDEDEV.cfg"
I have the config file in another folder and exe is in a different path. I'm new to power shell, how do I bypass that space between "C:\Program Files (x86)\Unisys\WebEnabler\Web Enabler.exe" and "configfile=c:\development-installs\web-enabler-config\CLMCPDEDEV.cfg"? That's the full short cut path when I do this manually. Any guidance is much appreciated.
Quote from CreateShortcut method documentation:
A common problem is putting arguments in the TargetPath property of
the shortcut object, which doesn't work. All arguments to the shortcut
must be put in the Arguments property.
So you have to do this:
$wshshell = New-Object -ComObject WScript.shell
$desktop = [System.Environment]::GetFolderPath('Desktop')
$lnk = $wshshell.CreateShortcut((Join-Path -Path $desktop -ChildPath 'CLMCPDEDEV.lnk'))
$lnk.TargetPath = 'C:\Program Files (x86)\Unisys\WebEnabler\Web Enabler.exe'
$lnk.Arguments = 'configfile=c:\development-installs\web-enabler-config\CLMCPDEDEV.cfg'
$lnk.Save()

Powershell Create new shortcut named after website

I'm trying to make shortcuts from Websites. There is no problem and it works with this Code:
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("E:\Powershell\Ziel\TestFile.lnk")
$Shortcut.TargetPath = "https://www.google.ch/"
$Shortcut.Save()
Now I want it to read the URL and create the shortcut with the name "Google" ("www.google.ch" would also be okay).
I'm also trying to read the URL out of my browser, and create the shortcut from that. What I mean is, when I'm on Google, I want to start the script and have it create a shortcut named "Google".
The important part is the first part.
You could use regex or String.IndexOf() and String.Substring() to grab the hostname from the url.
But the easiest (and most robust) way is probably to parse the URL as a [uri] type object, and then grab the Authority part:
function New-WebsiteShortcut
{
param(
[Parameter(Mandatory)]
[uri]$URL,
[Parameter()]
[ValidateScript({Test-Path $_ -PathType Container})]
[string]$Location = (Join-Path $env:USERPROFILE "Desktop")
)
# Grab hostname
$HostName = $URL.Authority
# construct file name and path from hostname
$ShortcutName = '{0}.lnk' -f $HostName
$ShortcutPath = Join-Path $Location $ShortcutName
# create shortcut
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = "$URL"
$Shortcut.Save()
}
Now you can supply just the URL (and optionally a location) to your new cmdlet:
New-WebsiteShortcut -URL "https://www.google.ch" -Location "E:\PowerShell\Ziel"
I'll leave error handling of existing file names as an exercise to the reader :)

Issues with Target Path in powershell in creating a short cut

I am trying to create a short cut on public desktop for users, but this target path for this short cut is causing some issues.
$wshshell = New-Object -ComObject WScript.shell
$desktop = [System.Environment]::GetFolderPath("desktop")
$lnk = $wshshell.CreateShortcut("$desktop\CLMCPDEDEV.lnk")
$lnk.TargetPath = "C:\Program Files (x86)\Unisys\WebEnabler\Web Enabler.exe" "configfile=c:\development-installs\web-enabler-config\CLMCPDEDEV.cfg"
I have the config file in another folder and exe is in a different path. I'm new to power shell, how do I bypass that space between "C:\Program Files (x86)\Unisys\WebEnabler\Web Enabler.exe" and "configfile=c:\development-installs\web-enabler-config\CLMCPDEDEV.cfg"? That's the full short cut path when I do this manually. Any guidance is much appreciated.
Quote from CreateShortcut method documentation:
A common problem is putting arguments in the TargetPath property of
the shortcut object, which doesn't work. All arguments to the shortcut
must be put in the Arguments property.
So you have to do this:
$wshshell = New-Object -ComObject WScript.shell
$desktop = [System.Environment]::GetFolderPath('Desktop')
$lnk = $wshshell.CreateShortcut((Join-Path -Path $desktop -ChildPath 'CLMCPDEDEV.lnk'))
$lnk.TargetPath = 'C:\Program Files (x86)\Unisys\WebEnabler\Web Enabler.exe'
$lnk.Arguments = 'configfile=c:\development-installs\web-enabler-config\CLMCPDEDEV.cfg'
$lnk.Save()

How to create a shortcut using PowerShell

I want to create a shortcut with PowerShell for this executable:
C:\Program Files (x86)\ColorPix\ColorPix.exe
How can this be done?
I don't know any native cmdlet in powershell but you can use com object instead:
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk")
$Shortcut.TargetPath = "C:\Program Files (x86)\ColorPix\ColorPix.exe"
$Shortcut.Save()
you can create a powershell script save as set-shortcut.ps1 in your $pwd
param ( [string]$SourceExe, [string]$DestinationPath )
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($DestinationPath)
$Shortcut.TargetPath = $SourceExe
$Shortcut.Save()
and call it like this
Set-ShortCut "C:\Program Files (x86)\ColorPix\ColorPix.exe" "$Home\Desktop\ColorPix.lnk"
If you want to pass arguments to the target exe, it can be done by:
#Set the additional parameters for the shortcut
$Shortcut.Arguments = "/argument=value"
before $Shortcut.Save().
For convenience, here is a modified version of set-shortcut.ps1. It accepts arguments as its second parameter.
param ( [string]$SourceExe, [string]$ArgumentsToSourceExe, [string]$DestinationPath )
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($DestinationPath)
$Shortcut.TargetPath = $SourceExe
$Shortcut.Arguments = $ArgumentsToSourceExe
$Shortcut.Save()
Beginning PowerShell 5.0 New-Item, Remove-Item, and Get-ChildItem have been enhanced to support creating and managing symbolic links. The ItemType parameter for New-Item accepts a new value, SymbolicLink. Now you can create symbolic links in a single line by running the New-Item cmdlet.
New-Item -ItemType SymbolicLink -Path "C:\temp" -Name "calc.lnk" -Value "c:\windows\system32\calc.exe"
Be Carefull a SymbolicLink is different from a Shortcut, shortcuts are just a file. They have a size (A small one, that just references where they point) and they require an application to support that filetype in order to be used. A symbolic link is filesystem level, and everything sees it as the original file. An application needs no special support to use a symbolic link.
Anyway if you want to create a Run As Administrator shortcut using Powershell you can use
$file="c:\temp\calc.lnk"
$bytes = [System.IO.File]::ReadAllBytes($file)
$bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON (Use –bor to set RunAsAdministrator option and –bxor to unset)
[System.IO.File]::WriteAllBytes($file, $bytes)
If anybody want to change something else in a .LNK file you can refer to official Microsoft documentation.