I'm trying to automate some stuff for users and one of them is to add "Computer" and "Documents" shortcut to their desktop.
I found the code below online and changed the target to "explorer.exe /e,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut(C:\users\username\Desktop\Computer.lnk")
$Shortcut.TargetPath = "explorer.exe \/e,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
$Shortcut.Save()
But when I run this code I get the following error :
"Exception calling "Save" with "0" argument : "Unable to save shotcut"
If there is another and easy way, I would love to hear it :)
Thank you everyone in advance.
I think i have a nice solution here. Also, in my own ineptitude, I think i figured out your error cause
First I found a cleaner way to make shortcuts to special folders.
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\users\user\Desktop\MacadizamianNizzut.lnk")
$Shortcut.TargetPath = [environment]::getfolderpath("mycomputer")
$Shortcut.Save()
You could also use mydocuments in place of mycomputer. For a complete list of special folders that you can use: [enum]::GetNames([System.Environment+SpecialFolder]). Tips hat to JRV for a comment on my link above.
As for your error "Exception calling "Save" with "0" arguments : "Unable to save shortcut". I also got that error. In practice it was because the value passed for createshortcut was not a valid path. I am not saying that the file has to exist but the folder path does. I made a typo and got the error. Using my example this command would have failed: Test-Path ""C:\users\user\Desktop"
Some Error Prevention
What we could do is assign the shortcut path to a variable and test the path based on that.
$ShortcutPath = "C:\users\username\desktop\test.lnk"
If(Test-Path -Path (Split-Path -Path $ShortcutPath -Parent)){
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = [environment]::getfolderpath("mycomputer")
$Shortcut.Save()
} Else {
Write-Host "Unable to create shortcut. Check the path $ShortcutPath."
}
Related
Sorry if this has been asked before but I can't find a relevant answer
I want to create a shortcut on a users desktop and fill in the Start In value on in.
I can create the Icon but how do I make powershell fill in the Start in value
$TargetFile = "C:\Program Files (x86)\Program"
$ShortcutFile = "$env:Public\Desktop\Shorcut.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.Save()
You can fill in the "Start in" value by simply adding this line:
$Shortcut.WorkingDirectory = "Path"
I have a UWP app I created and want to use powershell to create a shortcut on the desktop.
Creating a shortcut is easy for an exe
$TargetFile = "\Path\To\MyProgram.exe"
$ShortcutFile = "$env:USERPROFILE\Desktop\MyShortcut.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.Save()
But I'm struggling with what to use as the Target for the Universal apps.. I also know I can easily create a shortcut to an app manually but for this purpose, it needs to be done with PowerShell.. any ideas?
Creating shortcut for UWP app is a different story from classic desktop. You can refer to my another answer Where linked UWP tile?
To create a shortcut of an UWP app on the desktop using powershell, you can for example code like this:
$TargetFile = "C:\Windows\explorer.exe"
$ShortcutFile = "$env:USERPROFILE\Desktop\MyShortcut.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.Arguments="shell:AppsFolder\Microsoft.SDKSamples.AdventureWorks.CS_8wekyb3d8bbwe!App"
$Shortcut.TargetPath = $TargetFile
$Shortcut.Save()
You can find the AppUserModelId using the method in the link provided by #TessellatingHeckler, and replace the Microsoft.SDKSamples.AdventureWorks.CS_8wekyb3d8bbwe!App in the code with your desired AppUserModelId.
To prevent your shortcut from having the standard Explorer icon. Change the $Shortcut.TargetPath like this :
$TargetPath = "shell:AppsFolder\Microsoft.Windows.Cortana_cw5n1h2txyewy!CortanaUi"
$ShortcutFile = "$Home\Desktop\Cortana.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetPath
$Shortcut.Save()
This way the shortcut will have the same icon as the application.
You can also create a shortcut *.url via URI (if the application in question supports it) (https://learn.microsoft.com/en-us/windows/uwp/launch-resume/launch-default-app) :
$TargetPath = "ms-cortana:"
$ShortcutFile = "$Home\Desktop\Cortana.url"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetPath
$Shortcut.Save()
To determine the correct TargetPath https://www.tenforums.com/software-apps/57000-method-open-any-windows-10-apps-command-line.html documents the required steps:
List all applications via Powershell:
get-appxpackage > %TEMP%\application_list.txt
Open list:
notepad %TEMP%\application_list.txt
Find the PackageFamilyName for your app and navigate to the application's InstallLocation using Windows Explorer
Opening the AppxManifest.xml shows the Application ID: <Application Id="Microsoft.WinDbg.DbgSrv64" for the executable.
Combine the PackageFamilyName!ApplicationID to form your TargetPath
Microsoft.WinDbg_8wekyb3d8bbwe!Microsoft.WinDbg.DbgSrv64 for WinDbg Preview for instance.
I don't remember where I got this code from, but it's the best I know for finding a path ...
Powershell
$installedapps = get-AppxPackage
foreach ($app in $installedapps)
{
foreach ($id in (Get-AppxPackageManifest $app).package.applications.application.id)
{
$line = $app.Name + " = " + $app.packagefamilyname + "!" + $id
echo $line
}
}
... and there is an addition for this
How to avoid error when Get-AppxPackageManifest not found
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 :)
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()
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.