Powershell creating shortcut - powershell

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

Related

Creating a shortcut passing arguments to shell32.dll using Powershell

So I am trying to make a 'create shortcut' script to generate a link on my users desktop to take them directly to the "Add Printer Wizard" in Windows 10. Programatically speaking, I am pretty sure it is not possible but it is a directive from above. When the script runs, the Arguments field get dropped.
UPDATE
I can create this manually, but not programatically.
Help Me StackOverFlow ... You're our only hope
$sArguments = "shell32.dll,SHHelpShortcuts_RunDLL PrintersFolder"
$AppLocation = "C:\Windows\System32\rundll32.exe"
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:PUBLIC\Desktop\UAT Devices and Printers.lnk")
$Shortcut.TargetPath = $AppLocation
$Shortcut.Arguments = $sArguments
$Shortcut.IconLocation = "devicecenter.dll,0"
$Shortcut.Description ="UAT Devices and Printers"
$Shortcut.WorkingDirectory ="C:\Windows\System32"
$Shortcut.Save()
I feel stupid asking, but can anyone see what I am missing?
If you simply want to trigger the Add Printer Driver Wizard, here's the fixed version of your code (the arguments are differents, ref: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/rundll32-printui):
$sArguments = "printui.dll,PrintUIEntry /id"
$AppLocation = "C:\Windows\System32\rundll32.exe"
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:PUBLIC\Desktop\UAT Devices and Printers.lnk")
$Shortcut.TargetPath = $AppLocation
$Shortcut.Arguments = $sArguments
$Shortcut.IconLocation = "devicecenter.dll,0"
$Shortcut.Description ="UAT Devices and Printers"
$Shortcut.WorkingDirectory ="C:\Windows\System32"
$Shortcut.Save()

Create a Powershell shortcut with start in path

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"

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 Naming shortcut

$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"))))

How can I create a desktop shortcut for a Windows 10 Universal app using powershell?

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