0

I would like to insert environment variable %username% in this powershell script:

$o = new-object -com shell.application
$o.Namespace('\\xx.local\Employee Personal Folders\Employee\%username%\Scan Folder').Self.InvokeVerb("pintohome")

How can I go about that?

Cheers in advance

nopassport1
  • 1,821
  • 1
  • 25
  • 53
Derrick T
  • 15
  • 6
  • 1
    Does this answer your question? [How do I get the current username in Windows PowerShell?](https://stackoverflow.com/questions/2085744/how-do-i-get-the-current-username-in-windows-powershell) – Jory Geerts Feb 05 '20 at 11:31

1 Answers1

1

PowerShell uses a different format for environment variables. For example, %USERNAME% is specified as $env:USERNAME. Try updating your code as follows:

$o = new-object -com shell.application
$o.Namespace("\\xx.local\Employee Personal Folders\Employee\$env:USERNAME\Scan Folder").Self.InvokeVerb("pintohome")

Note the double-quotes around the path string - this is required for PowerShell to automatically replace the variable.

boxdog
  • 7,894
  • 2
  • 18
  • 27
  • Cool! I actually did some research prior to asking my question and I did try the $env:Username. However, I was using ' ' rather than " ". Causing the script to fail. – Derrick T Feb 05 '20 at 11:36