Saturday, August 25, 2007

Restart your engine - The PowerShell way (updated)

This is an update to my first blog post. Now it will open PowerShell via its shortcut file.
Although the first version worked fine, it fired PowerShell with a black background and the window looked very similar to the old DOS prompt. 

You can customize the Windows PowerShell shortcut file (under Start > Programs > Windows PowerShell 1.0 ) to your favorite working folder, colors, dimensions etc. Include the function in your $PROFILE and type "rps" to restart.

function Restart-PowerShell {
$cmd = @"
'This is a temporary script to Restart a PowerShell Session
'Created $(Get-Date)
On Error Resume Next
Set oApp = CreateObject("Shell.Application")
Set oShell = CreateObject("WScript.Shell")
lnk = "%ALLUSERSPROFILE%\Start Menu\Programs\Windows PowerShell 1.0"
PoSH = oShell.ExpandEnvironmentStrings(lnk)
WSCript.Sleep 1000
oApp.Namespace(PoSH).ParseName("Windows PowerShell.lnk").InvokeVerb("Open")

out-file $env:temp"\Restart-PowerShell.vbs" -inputobject $cmd -encoding ASCII -force;
WScript.exe $env:temp"\Restart-PowerShell.vbs";

set-alias rps Restart-PowerShell


Anonymous said...

Using a vbscript is not really "the powershell way". Try this:

invoke-expression "$env:ALLUSERSPROFILE\Microsoft\Windows\Start Menu\Programs\Windows PowerShell 1.0\Windows PowerShell.lnk"

Shay Levy said...

Hi Anonymous

Admittedly, the title is a bit tricky, but you're missing the point.
Consider this, can you execute PowerShell where no instances of PowerShell exists with PowerShell commands?.

Thanks for commenting.


Anonymous said...

Sorry, my mistake. Should be invoke-item, not invoke-expression.

function restart-powershell
invoke-item "$($env:ALLUSERSPROFILE)\Microsoft\Windows\Start Menu\Programs\Windows PowerShell 1.0\Windows PowerShell.lnk"