Run powershell to capture variable and return to command line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Run powershell to capture variable and return to command line

#1 Post by atfon » 27 Oct 2017 14:45

Please forgive my ignorance if this has been answered elsewhere. I am trying to create a batch script that will utilize PowerShell to prompt for a folder. When the folder has been selected, the location will be saved as a variable to be used later. At this point, I would like the script to return to the command line. Instead, it is remaining in PowerShell. Is there a way to exit the PowerShell after obtaining the variable? I will include a sample simplified version of the code:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose your folder.',0x270,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "myfolder=%%I
echo !myfolder!
pause>nul
exit

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Run powershell to capture variable and return to command line

#2 Post by aGerman » 27 Oct 2017 15:00

To be honest I don't understand your question. When I execute your script the window title never changed to something other than C:\Windows\system32\cmd.exe. Only the BrowseForFolder method will be executed in Powershell. What let you assume that the Powershell instance doesn't quit after you selected a folder? Obviously ECHO and PAUSE are executed in your Batch script again.

Steffen

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Run powershell to capture variable and return to command line

#3 Post by atfon » 27 Oct 2017 15:03

Hello Steffen,

Thank you for your post.

What let you assume that the Powershell instance doesn't quit after you selected a folder?


On my system, the title bar changed to "Windows PowerShell" and never changed back.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Run powershell to capture variable and return to command line

#4 Post by aGerman » 27 Oct 2017 15:10

Strange. Maybe -NoProfile will do the trick.

Code: Select all

for /f "usebackq delims=" %%I in (`powershell -NoProfile -ExecutionPolicy Bypass %psCommand%`) do set "myfolder=%%I

Steffen

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Run powershell to capture variable and return to command line

#5 Post by atfon » 27 Oct 2017 15:25

I tried the -NoProfile option, but the issue persisted. I am running Windows 7 and the batch commands are running as Administrator. I don't know if that makes any difference in behavior.

As a test, I tried a command which should produce an error in PowerShell, but produce results in the command line (ver). It is correctly displaying the operating system version at this point. Very curious.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Run powershell to capture variable and return to command line

#6 Post by aGerman » 27 Oct 2017 15:36

If I right-click the file and run as admin nothing changes (except the "Administrator:" prepended in the title bar :wink:) It's Windows 10.

Steffen

D:\test is what I have chosen ...
Screenshot.png
Screenshot.png (10.65 KiB) Viewed 12789 times

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Run powershell to capture variable and return to command line

#7 Post by Squashman » 27 Oct 2017 16:03

I get the same results as aGerman. Windows 7.

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Run powershell to capture variable and return to command line

#8 Post by atfon » 27 Oct 2017 16:31

I just tested on another system running Windows XP (with PowerShell installed, obviously) and I am seeing the same behavior as the two of you. The title bar remains as cmd.exe. I wonder might be causing the title bar information to change on the Windows 7 system. Anyhow, you guys are the experts. As long as you think the title bar continuing to list PowerShell does not imply any possible complications with continuing on with the script, I'm satisfied.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Run powershell to capture variable and return to command line

#9 Post by Aacini » 27 Oct 2017 21:31

I am afraid I don't understand what exactly is the question. However, if it is related to how the cmd.exe window title changes when powershell starts and ends, then perhaps this SO question may give some information. The conclusion is that this point depends on the powershell version...

Antonio

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Run powershell to capture variable and return to command line

#10 Post by atfon » 28 Oct 2017 00:10

I apologize if I have not been clear about my question previously. When my script is run on a Windows 7 system, the title bar changes from cmd.exe to PowerShell. When it returns to continue executing the rest of the batch script, the title bar does not change back to cmd.exe. My concern was the script was not properly exiting the PowerShell portion of the script and returning to the command line. Other parties here have not been able to replicate the behavior and I wasn't on another system running a different OS, to be sure. If you folks do not think this should be a concern, I will not worry further about it, unless I notice any unexpected behavior (other than the title bar). You are really the experts. I'm merely relying on your greater expertise. I hope that helps to clarify the inquiry.

Thanks again.

Post Reply