thuelsing.de THUELSING.DE

Techstuff, programming and galleries

PowerShell

After the tips about batch commandos, I've decided to have a closer look at the windows powershell. First of all, it is annoying, that the width is limited. Working deep down the folder hierarchy, more or less all commands get truncated.

The scripting guy wrote a nice and funny example how to change it (Check this). I came up with a slightly changed version of it:

function Global:Set-MaxWindowSize

{

    if ($Host.Name -match "console")

       {

        $MaxHeight = $host.UI.RawUI.MaxPhysicalWindowSize.Height

        $MaxWidth = $host.UI.RawUI.MaxPhysicalWindowSize.Width

 

        $MyBuffer = $Host.UI.RawUI.BufferSize

        $MyWindow = $Host.UI.RawUI.WindowSize

    

        $MyWindow.Height = ($MaxHeight)

        $MyWindow.Width = ($Maxwidth-4)

 

        $MyBuffer.Height = (9999)

        $MyBuffer.Width = ($Maxwidth-4)

 

        $host.UI.RawUI.set_bufferSize($MyBuffer)

        $host.UI.RawUI.set_windowSize($MyWindow)

       }

}

 

Now, you can resize the window by calling the function. Just put the code in your profile.

If you want to change the default, simply change the sizes to your needs and rename the function. Include the function call below the definition in your profile and the powershell will open and resize immediately.

Since I was searching the internet for tweaks anyway, I found a few lines to rename the powershell window properly:

 

$CurrentUser = [Security.Principal.WindowsIdentity]::GetCurrent()

$CurrentUserPrincipal = New-Object Security.Principal.WindowsPrincipal $CurrentUser

$Adminrole = [Security.Principal.WindowsBuiltinRole]::Administrator

If (($CurrentUserPrincipal).IsInRole($AdminRole)){$Elevated = "Administrator"}   

 

$Title = $Elevated + " $ENV:USERNAME".ToUpper() + ": $($Host.Name) " + $($Host.Version) + " - " + (Get-Date).toshortdatestring() 

$Host.UI.RawUI.set_WindowTitle($Title)

 

You can add or remove any variable you want.

 

Last but not least, I wanted to have the option to open the powershell in a given location. You could either write “powershell” in the address bar of the explorer or add another registry entry (like the one for emacs):

Create file called something.reg and include:

 

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\PSOpenHere]

@="PowerShell Here"

[HKEY_CLASSES_ROOT\Directory\shell\PSOpenHere\command]

@="C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%L'"

 

Run it afterwards and enjoy an additional context menu entry “PowerShell here”! I’ve added the file here.