Tuesday 5 May 2015

Powershell call exe using batch file

Copy exe in to a folder: Ex:C:\Deploy\MyProject\
My exe name: MyProject.exe

C:\Deploy\MyProject\MyProject.exe

Now Write PS1 file code as: My.PS1

My.PS1
--------------
Param
(
[string]$pathofexe = $null
)

$pathofexe= $pathofexe
& $pathofexe

---------------------

Save above PS1 in folder C:\Deploy\PS

Now write .bat file

My.bat

------------------------
@Set pathofexe=%~dp0MyProject\MyProject.exe

PowerShell.exe Set-ExecutionPolicy RemoteSigned; %~dp0PS\My.PS1 -pathofexe %pathofexe%

PAUSE
------------------------

Save above .bat file in folder C:\Deploy

Now your file structure:
MyProject.Exe -C:\Deploy\MyProject\MyProject.exe
My.PS1- C:\Deploy\PS\My.PS1
My.bat - C:\Deploy\My.bat

Now run My.bat as administrator


Note: If you get any error while running above try with: Set-ExecutionPolicy unrestricted,

PowerShell.exe Set-ExecutionPolicy unrestricted; %~dp0PS\My.PS1 -pathofexe %pathofexe%