PowerShell Desktop Variable

bgregor picture bgregor · Jul 31, 2015 · Viewed 54.5k times · Source

I am trying to write a PowerShell script to remove the desktop icon for Chrome after installing through sccm. However, certain users in the network have their desktop directed to different folders on the network. Is there a variable in PowerShell that stores the location of the desktop?

I have looked online and searched using Get-Variable | Out-String, but I didn't find anything. The finished code should look like:

If (Test-Path "$DesktopLocation\Google Chrome.lnk"){
    Remove-Item "$DesltopLocation\Google Chrome.lnk"
}

Answer

Mathias R. Jessen picture Mathias R. Jessen · Jul 31, 2015

You can use the Environment.GetFolderPath() method to get the full path to special folders:

$DesktopPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop)

This can be shortened to:

$DesktopPath = [Environment]::GetFolderPath("Desktop")

You can also get the "AllUsers" shared desktop folder (if the shortcut file is shared among all users):

[Environment]::GetFolderPath("CommonDesktopDirectory")

Check out the full list of values for the SpecialFolder Enum.