Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Desktop Variable

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" } 
like image 763
bgregor Avatar asked Jul 31 '15 13:07

bgregor


People also ask

How do I get the Desktop Path in PowerShell?

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.

What is the $_ variable in 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.

Can you use variables in PowerShell?

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.


2 Answers

If you need $Desktop\a.txt, use this

echo ([Environment]::GetFolderPath("Desktop")+"\a.txt") 
like image 38
YH Wu Avatar answered Sep 21 '22 12:09

YH Wu


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.

like image 136
Mathias R. Jessen Avatar answered Sep 18 '22 12:09

Mathias R. Jessen