Solved

Trying to run powershell  in a batch file. How do I do this?

Posted on 2016-10-28
8
125 Views
Last Modified: 2016-11-11
I have a simple powershell script. I like to make this as a batch file so that a user just double clicks and run. It has a prompt to enter user id.

$userid= Read-Host "Enter A Username To Reset Settings"
$src="c:\temp\ugl\"+$userid
$dest="c:\temp\ugl2\"+$userid
robocopy $src $dest /MIR

Open in new window


When I run a batch file with follow, it just blinks cmd window and close.

powershell.exe  -noexit -executionpolicy ByPass -File "C:\scripts\file-replace.ps1"

Open in new window

0
Comment
Question by:crcsupport
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 350 total points
ID: 41864303
Save the PowerShell script as a .ps1 file and run it in using PowerShell.exe from a batch file..
@ECHO OFF
PowerShell.exe -ExecutionPolicy Bypass -Command "& 'C:\scripts\Test.ps1'"
PAUSE

Open in new window

Or
@ECHO OFF
PowerShell.exe -ExecutionPolicy Bypass -File "C:\scripts\Test.ps1"
PAUSE

Open in new window

Ref : http://ss64.com/ps/powershell.html

PS : remove PAUSE command if you want to automatically close the batch file..
1
 
LVL 1

Author Comment

by:crcsupport
ID: 41864307
This is what it  returns;
C:\scripts>∩╗┐powershell.exe  -noexit -executionpolicy ByPass -File "C:\scripts\file-replace.ps1"
'powershell.exe' is not recognized as an internal or external command,
operable program or batch file.
0
 
LVL 1

Author Comment

by:crcsupport
ID: 41864310
Wait a minute, after I put @ECHO OFF, it recognizes Powershell. Why does this make different?
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 40

Expert Comment

by:Subsun
ID: 41864333
I don't think adding @echo off would have fixed it..
'powershell.exe' is not recognized as an internal or external command,
Probably you are missing an environment variable in your computer.

Add this to your Path environment variable:
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\

Or Try..
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -executionpolicy ByPass -File "C:\scripts\file-replace.ps1"

Open in new window

0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 150 total points
ID: 41864360
I think you have stored the  batch file as Unicode or UTF8 instead of ANSI/ASCII.
0
 
LVL 1

Author Comment

by:crcsupport
ID: 41864439
I have no problem with ENV.
I just tested again, it's the same. I have to put @echo off so that the batch file recognizes powershell.exe.

Yes, I saved it as unicode. What makes difference saving unicode and ascii
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41864731
Unicode batch files require an additional option in cmd.exe when getting called. Depending on the convention used, and I can't recall which one applies here, the first two bytes of the file contain a special bytes sequence to identify the low-to-high bits sequence, and that looks like the special characters you've shown.

As a rule of thumb, always run cmd batches in ASCII mode.
2
 
LVL 6

Expert Comment

by:Kevin Stanush
ID: 41883281
fyi, BOM for Unicode is 0xFEFF.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question