How do you do the equivalent of using a remote server's $env namespace without hardcoding network paths for cmdlets like Set-Location? I have a script that loops through remote servers and I'm trying to access their equivalent of $env:programfiles from a single script, but these servers have the variable set in different locations.
Basically getting a loop that navigates
c:\program files
\\server1\c$\program files
\\server2\c$\programs
\\server3\d$\apps
Using something familiar and simple like
Set-Location "${env:programfiles}"
And have the remote server's $env return a network path instead of a drive letter. The current way I'm doing it is grabbing the paths using Invoke-Command and building the path manually (replace : with $, append \\server in front of the path, turning "c:\program files" to "\\server\c$\program files")
Best Answer
This might be a little less complicated, and can easily be thrown into a
foreach
loop to work through your list of servers.Here is screenshot of the output: