Solved

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

Posted on 2016-10-28
8
63 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
  • 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
 
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 68

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 68

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 4

Expert Comment

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

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now