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

3
  • You didn't say what wasn't working. Where in the script are you using the [Environment]::UserName and what happens? Commented Jul 6, 2016 at 15:47
  • $user = [Environment]::UserName and I'm trying to use $user as a variable in a path. It returns blank Commented Jul 6, 2016 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. Commented Jul 7, 2016 at 1:53

1 Answer 1

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 
1
  • 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. Commented Jul 6, 2016 at 23:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.