Share via


How to get variables from a .bat file launching a powershell script

Question

Thursday, September 8, 2011 1:27 AM

I have a legacy scripting solution that relies on .bat files. I am slowly trying to insert Powershell into the mix to resolve the issue, but, I can't just go powershell across the board yet.  So, I need to find a way to get access to the variables present in the cmd shell instance that launches the powershell script.  For example, a new variable %rootdrive% is created when the .bat file runs.  Later in the .bat file, I will call powershell.exe and I want to be able to reference this %rootdrive% variable (and value).

All replies (8)

Thursday, September 8, 2011 1:33 AM ✅Answered

Can you use it as an argument to a called script?

powershell.exe -file c:\scripts\somescript.ps1 %rootdrive%

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Thursday, September 8, 2011 2:17 AM ✅Answered

Hey Will,

You can access local environment variables from with-in PowerShell provided the PowerShell session was started from the DOS session that contains the environment variable. For example take the following:

 

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\> name=crazyadam
C:\> echo %name%
crazyadam
C:\> powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\> $env:name
crazyadam

Lets talk about what I just did. I created a session variable "name" with value "crazyadam". I started PowerShell, and I accessed my local environment variable from with-in PowerShell using the environment provider. The environment provider is accessed like a drive using "$env". The "about" page for environment variables will explain it all better than I can.

about_Environment_Variables

http://technet.microsoft.com/en-us/library/dd347713.aspx

Good luck bro!

~CA

Adam


Thursday, September 8, 2011 2:10 AM

Can you use it as an argument to a called script?

powershell.exe -file c:\scripts\somescript.ps1 %rootdrive%

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

Thanks.  I ended up using this approach and just pulled it off $args.


Thursday, September 8, 2011 2:32 AM

Thanks Adam.  That's even easier.  I need to take a week and reread all those help files one of these days.


Thursday, September 8, 2011 10:59 AM

Using global variables in Powershell is considered bad practice, and to be avoided if at all possible, because of the potential for unintended consequences.  While I agree it's frequently convenient I'd think using environment variables would have that same potential.[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Thursday, September 8, 2011 6:48 PM

It really pays benefits to read the PowerShell help texts fully and

carefully.

 

I'm not fond of how they are formatted, so I'm slowly creating a set of

them formatted to my own tastes. At least that exercise makes me read them.

 


Friday, September 9, 2011 11:12 AM

Using global variables in Powershell is considered bad practice, and to be avoided if at all possible, because of the potential for unintended consequences.  While I agree it's frequently convenient I'd think using environment variables would have that same potential. [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

Mjolinor, I am gathering I should use the parameter/argument approach to put the value into the proper scope and use the function scope boundary.  Is that correct?


Friday, September 9, 2011 11:39 AM

If you pass it as an argument, scope doesn't matter.  The argument will be passed into whatever scope the script will be running in.

If you think about the process of converting a process that uses .bat files to using .ps1 scripts piecemeal, you're going to be relying on enviroment variables in some form - in this case %rootdrive%. 

Now, when you go to replace one of those .bat files in the stack with a .ps1 script, you can write the script to either use that value as a parameter, or you can write it to use that environment variable directly. At some point you're going to have all the .bat files converted and you're going to want to quit using those enviroment variables. 

If you write the the .ps1 scripts to use the environment variables now, you'll have to go back and re-write them later to pass values using ps variables. If you write them to use parameters now, that's already done and you don't have to go back through them looking for everywhere you used an environment variable and changing it.

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "