Worklet: Place Shortcut on Current User's Desktop

  • 10 March 2020
  • 5 replies
  • 345 views

Userlevel 4
Badge

Hi there.


I would like to deploy icons on the current user’s desktop. This is currently being done using group policy as part of the user’s logon. Is there a way to achieve this using Worklets?


Is there an evaluation script I can run to figureout the currently logged on user? Then a remediation script to run that will create the icons on the current logged on user’s desktop?


5 replies

Userlevel 7

There’s some code in this worklet that creates a shortcut:




that you could repurpose.


This worklet has code that will grab the current user:



Between the two you could make a worklet that grabs the current user, then puts a shortcut in their desktop folder. You’d want all of the code in the remediation section. For the evaluation you could use exit 1 to make it run every time, or you could check for the presence of the shortcut first.


Let me know if you have any issues getting that to work and I’m happy to help out.

Userlevel 4
Badge

Perfect! This is amazing. The information you have shared is enough for me to work out the necessary Powershell code. Thank you 😍

wootwoot! i have addressing this on my todo list, as well…while domain joined, many of my systems are beyond the consistent reach of GPO policies. 🤬🤮🤬


i would legit be interested to see how you wrote this script block out!?


how i attacked this: i broke this down into three unique blocks ([a] add a shortcut to a website for current user, [b] add a shortcut to an application for the current user, and [c] add a shortcut to a website for all users).


to place a shortcut to a website on the current user desktop:


# System variable
$CurrentUser = (Get-WmiObject -class win32_process | Where-Object name -Match explorer).getowner().user

# Variables :: define target, name, & location of your shortcut
$Target = "https://google.com"
$ShortcutName = "Google.url"
$Location = "c:\Users\${CurrentUser}\Desktop\"

# Work zone :: No variables/edits below

# if Windows 10 (due to user folder locations):
if ([Environment]::OSVersion.Version.Major -ge 10) {

# create a variable referencing a Wscript.Shell COM Object
$new_Shortcut = New-Object -ComObject WScript.Shell

# build target path for method and save the shortcut
$source_path = Join-Path -Path $Location -ChildPath $ShortcutName
$source = $new_Shortcut.CreateShortcut($source_path)
$source.TargetPath = $Target
$source.Save()
}

to place a shortcut to an application on the current user desktop:


# System variable
$CurrentUser = (Get-WmiObject -class win32_process | Where-Object name -Match explorer).getowner().user

# Variables :: define target, name, & location of your shortcut
$Target = "$env:SystemRoot\System32\notepad.exe"
$ShortcutName = "Notepad.lnk"
$Location = "c:\Users\${CurrentUser}\Desktop\"

# Work zone :: No variables/edits below

# if Windows 10 (due to user folder locations):
if ([Environment]::OSVersion.Version.Major -ge 10) {

# create a variable referencing a Wscript.Shell COM Object
$new_Shortcut = New-Object -ComObject WScript.Shell

# build target path to the method and save the shortcut
$source_path = Join-Path -Path $Location -ChildPath $ShortcutName
$source = $new_Shortcut.CreateShortcut($source_path)
$source.TargetPath = $Target
$source.Save()
}

to place a shortcut to a website on all user desktops:


# Variables :: define target & name of your shortcut
$Target = "$env:SystemRoot\System32\notepad.exe"
$ShortcutName = "Notepad.lnk"

# Work zone :: No variables/edits below

# if Windows 10 (due to user folder locations):
if ([Environment]::OSVersion.Version.Major -ge 10) {

# create a variable referencing a Wscript.Shell COM Object
$new_Shortcut = New-Object -ComObject WScript.Shell

# define the location and name of your shortcut
$Location = $new_Shortcut.SpecialFolders.Item("AllUsersDesktop")

# build target path to the method and save the shortcut
$source_path = Join-Path -Path $Location -ChildPath $ShortcutName
$source = $new_Shortcut.CreateShortcut($source_path)
$source.TargetPath = $Target
$source.Save()
}
Userlevel 4
Badge

I haven’t actually written the full script yet, but the key to achieving this falls into the following code snippets:


$strCurrentUserName = (Get-WmiObject -class win32_process -ComputerName 'localhost' | Where-Object name -Match explorer).getowner().user

This returns the currently logged on user’s name (this may go awry if there is more than one user logged on but it’s rare). It might make sense to check for this scenario and abort the script if it’s detected.


(Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain

This checks if the machine is in the domain. Not really necessary since the Automox agent is only installed on domain-joined computers in my case.


$strDNSDomainName = ((Get-WmiObject -Class Win32_ComputerSystem).Domain).ToUpper()

Returns the DNS name of the domain the computer is joined to. This is just an additional check to ensure the computer the Automox agent is running on is a corporate computer.


(Get-WmiObject -ComputerName localhost Win32_UserProfile) | select localpath

This allows allows me to dynamically generate the user profile path. It’s quite easy to iterate through all the paths returned and figure out which belongs to $strCurrentUserName

Userlevel 7

Glad that I was able to point you in the right direction @jesumyip!

Reply