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" }
To get the user's home folder, use $env:USERPROFILE in PowerShell and %userprofile% in Command Prompt/batch files. cd $env:USERPROFILE\Desktop will work for your PowerShell.
The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.
In PowerShell, variables are represented by text strings that begin with a dollar sign ( $ ), such as $a , $process , or $my_var . Variable names aren't case-sensitive, and can include spaces and special characters.
If you need $Desktop\a.txt, use this
echo ([Environment]::GetFolderPath("Desktop")+"\a.txt")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With