0

I'm trying to create a Shortcut for a powershell script that uses [Environment]::UserName the shortcut will be started from the correct user I want to read from env variable.

%SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe -NoLogo -ExecutionPolicy Bypass -noexit -File "C:\test\test.ps1"

This the target on the shortcut. How can I make Env variables work?

Thanks

  • You didn't say what wasn't working. Where in the script are you using the [Environment]::UserName and what happens? – Mike Shepard Jul 6 '16 at 15:47
  • $user = [Environment]::UserName and I'm trying to use $user as a variable in a path. It returns blank – rgomez Jul 6 '16 at 15:56
  • @rgomez I can't recreate the behavior your describing. an environment variable is still present even if I call a script from a shortcut. Is [Environment]::UserName used in your test.ps1 file? if so please show the file. I need more detail to help. – Eric Jul 7 '16 at 1:53
0

The powershell syntax i use for environment variables is

$env:username

If you are using it in a string or path you need to make sure the parsing order is correct.

$($env:username)

so if your username is user1 and you want path

C:\Users\user1\test.ps1

you would use

C:\Users\$($env:username)\test.ps1
| improve this answer | |
  • That's how I'm using it but when loading powershell from a shortcut I'm getting that enviroment variable empty. I guess because powershell load is made different way than when you go to prompt opening a regular powershell console. – rgomez Jul 6 '16 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.