Solved

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

Posted on 2016-10-28
8
115 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
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
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 69

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 69

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 5

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…

763 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