Avatar of rakkad
rakkad
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Powershell script running in a dos batch script

Hi

I have created a powershell script that works within powershell, but I need to run the script in command prompt in a dos batch script.  Can anyone help please ?

Thanks
PowershellWindows BatchMicrosoft DOS

Avatar of undefined
Last Comment
Ben Personick (Previously QCubed)

8/22/2022 - Mon
Ben Personick (Previously QCubed)

Hello Rakkad,

  In your batch file simply put a line in the cmd script calling the PowerShell like this:

CALL Powershell "[Drive\folder\folder\yourscript.ps1"

Open in new window


Usually I like to create a wrapper script file with the same name as the PowerShell script so that I can run it and log the output to a file similar to below:


:: Script Name: PureBackup.cmd
:: Version: 1.1.5
@(
	SETLOCAL
	ECHO OFF
	SET "eLvl=0"
	REM SET "Log=%~dp0log\%~n0_Output.log"
	SET "PS1CMD=%~dpn0.ps1"
	REM SET "_PSDebugLevel=SilentlyContinue"
	SET "_PSDebugLevel=Continue"
)

CALL :Main

(
	ENDLOCAL
	Exit /b %eLvl%
)

:Main

	CALL Powershell %PS1CMD% "%_PSDebugLevel%" >>"%Log%" 2>&1

GOTO :EOF

Open in new window

Muhammad Burhan

simply, you can call it within your batch script by adding
powershell -noexit & “C:\script.ps1”

Open in new window


ref:
https://poshoholic.com/2007/09/27/invoking-a-powershell-script-from-cmdexe-or-start-run/
Shaun Vermaak

Or you can combine them into one file
@Echo Off
PowerShell.exe -Command "Invoke-Expression -Command ((Get-Content -Path '%~f0' | Select-Object -Skip 3) -join [environment]::NewLine)"
Exit /b %ErrorLevel%
## Powershell starts here
Write-Host "Hello World"

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
rakkad

ASKER
How can I run powershell as an administrator in a dos batch command ?

Thanks
Qlemo

You can't - that would circumvent any measures introduced to not allow exactly for that. We can check if the batch script runs as admin though:
whoami /groups | find "S-1-16-12288" >nul ||  (echo *** Not running elevated - STOP *** & pause & exit /b 1)

Open in new window

ASKER CERTIFIED SOLUTION
Ben Personick (Previously QCubed)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Qlemo

"You can't" - because you do not use UAC with less than at least confirmation prompt :D.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Ben Personick (Previously QCubed)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.