• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How can I resolve %WINDIR% inside a PowerShell script?

S

ssg31415926

#1
I want to run this command from within a PowerShell script:

fsutil hardlink create %WINDIR%\system32\windowspowershell
\v1.0\psh.exe %WINDIR%\system32\windowspowershell\v1.0\powershell.exe

It works from with cmd.exe, as you'd expect, but not from within
powershell.exe. (I didn't really expect it to, though.)

It there a way to get powershell to resolve system environment
variables or do I need to retrieve them using PowerShell and build a
command line?
 

My Computer

S

ssg31415926

#2
On Sep 3, 10:44 am, ssg31415926 <newsjunkm...@xxxxxx> wrote:

> I want to run this command from within a PowerShell script:
>
> fsutil hardlink create %WINDIR%\system32\windowspowershell
> \v1.0\psh.exe %WINDIR%\system32\windowspowershell\v1.0\powershell.exe
>
> It works from with cmd.exe, as you'd expect, but not from within
> powershell.exe.  (I didn't really expect it to, though.)
>
> It there a way to get powershell to resolve system environment
> variables or do I need to retrieve them using PowerShell and build a
> command line?
Never mind. Found it: $env:windir, hence:

fsutil hardlink create $env:WINDIR\system32\windowspowershell
\v1.0\psh.exe $env:WINDIR\system32\windowspowershell
\v1.0\powershell.exe
 

My Computer

O

Oisin (x0n) Grehan [MVP]

#3
But if you ever did need to resolve one of those kind of environment
variables, you could do it like this:

PS> [System.Environment]::ExpandEnvironmentVariables("%WINDIR%")
C:\Windows

- Oisin


ssg31415926 wrote:

> On Sep 3, 10:44�am, ssg31415926 <newsjunkm...@xxxxxx> wrote:

> > I want to run this command from within a PowerShell script:
> >
> > fsutil hardlink create %WINDIR%\system32\windowspowershell
> > \v1.0\psh.exe %WINDIR%\system32\windowspowershell\v1.0\powershell.exe
> >
> > It works from with cmd.exe, as you'd expect, but not from within
> > powershell.exe. �(I didn't really expect it to, though.)
> >
> > It there a way to get powershell to resolve system environment
> > variables or do I need to retrieve them using PowerShell and build a
> > command line?
>
> Never mind. Found it: $env:windir, hence:
>
> fsutil hardlink create $env:WINDIR\system32\windowspowershell
> \v1.0\psh.exe $env:WINDIR\system32\windowspowershell
> \v1.0\powershell.exe
 

My Computer

S

ssg31415926

#4
On Sep 3, 3:33 pm, "Oisin (x0n) Grehan [MVP]" <ois...@xxxxxx>
wrote:

> But if you ever did need to resolve one of those kind of environment
> variables, you could do it like this:
>
> PS> [System.Environment]::ExpandEnvironmentVariables("%WINDIR%")
> C:\Windows
>
> - Oisin
>
> ssg31415926wrote:

> > On Sep 3, 10:44 am,ssg31415926<newsjunkm...@xxxxxx> wrote:

> > > I want to run this command from within a PowerShell script:
>

> > > fsutil hardlink create %WINDIR%\system32\windowspowershell
> > > \v1.0\psh.exe %WINDIR%\system32\windowspowershell\v1.0\powershell.exe
>

> > > It works from with cmd.exe, as you'd expect, but not from within
> > > powershell.exe. (I didn't really expect it to, though.)
>

> > > It there a way to get powershell to resolve system environment
> > > variables or do I need to retrieve them using PowerShell and build a
> > > command line?
>

> > Never mind.  Found it: $env:windir, hence:
>

> > fsutil hardlink create $env:WINDIR\system32\windowspowershell
> > \v1.0\psh.exe $env:WINDIR\system32\windowspowershell
> > \v1.0\powershell.exe
That could be handy. Thanks.
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)