Wednesday, February 8, 2012

Self Running PowerShell File

Will (http://learningpcs.blogspot.com/) and I had some email back and forth today about custom installers using PowerShell.  In this correspondence, I was trying to think of the most intuitive way to execute a PowerShell script on an unknown system.

One of these, I thought was academically interesting... and Hence, the PowerShell batch script.

Save this to a batch file : Test.bat

<#
Cls
copy %~dpn0.BAT %~dpn0.ps1
powershell.exe -Noprofile -ExecutionPolicy Unrestricted -file %~dpn0.ps1
::@Pause
@DEL %~dpn0.ps1
@GOTO :EOF
#>
("1+1="+(1+1))



Now the issue is that it is making a copy of itself.... but it will run.

- EDIT - Had an error at work, see http://import-powershell.blogspot.com/2012/02/another-ps1batch-approach.html