Last evening I saw a tweet passing by from @pcgeek86 asking if it was possible to automate the width and height of the PowerShell console… in other words: manipulation of the PowerShell console.
Yes, this is possible by using the $host.UI.RawUI capabilities.
For your convenience I’ve written this function so that some basic manipulation of the PowerShell console can be done the easy way… I hope you find it useful
function Set-Console
{
<#
.SYNOPSIS
Function to be used for PowerShell console manipulation..DESCRIPTION
This function can be used for the manipulation of the PowerShell console.
For example, setting the width, height, backgroundcolor, etc..PARAMETER Width
Provide the width for the PowerShell console..PARAMETER Height
Provide the height for the PowerShell console..PARAMETER ForegroundColor
Possible values: Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White..PARAMETER BackgroundColor
Possible values: Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White..PARAMETER Title
To set the title of the PowerShell console..PARAMETER X
Provide the location for the PowerShell console on the X-axis..PARAMETER Y
Provide the location for the PowerShell console on the Y-axis..NOTES
Author: Jeff Wouters | Methos IT.EXAMPLE
Set-Console -Width 140 -Height 60 -Title “My Window Title” -ForegroundColor Cyan -BackgroundColor Magenta -X 10 -Y 30
#>
param (
[Parameter(Position=0, Mandatory=$True)][String]$Width,
[Parameter(Position=1, Mandatory=$True)][String]$Height,
[Parameter(Position=2, Mandatory=$True)][Alias(‘Foreground’)][String]$ForegroundColor,
[Parameter(Position=3, Mandatory=$True)][Alias(‘Background’)][String]$BackgroundColor,
[Parameter(Position=4, Mandatory=$True)][Alias(‘WindowTitle’)][String]$Title,
[Parameter(Position=5, Mandatory=$True)][Alias(‘PositionX’)][String]$X,
[Parameter(Position=5, Mandatory=$True)][Alias(‘PositionY’)][String]$Y
)
$MaximumSize = $Host.UI.RawUI.MaxWindowSize
if ( ( $MaximumSize.height -ge $Height ) -and ( $MaximumSize.width -ge $Width ) )
{
$WindowSize = $host.UI.RawUI.WindowSize
$WindowSize.Height = $Height
$WindowSize.Width = $Width
$Host.UI.RawUI.Set_WindowSize($WindowSize)
}
else
{
Write-Output “Error: The proposed size is larger than the buffer.”
}
if ( $ForegroundColor )
{
$Host.UI.RawUI.Set_ForegroundColor($ForegroundColor)
}
if ( $BackgroundColor )
{
$Host.UI.RawUI.Set_BackgroundColor($BackgroundColor)
}
if ( $Title )
{
$Host.UI.RawUI.Set_WindowTitle($Title)
}
if ( ( $X ) -and ( $Y ) )
{
$windowPosition = $Host.UI.RawUI.WindowPosition
$WindowPosition.Y = $Y
$WindowPosition.X = $X
$Host.UI.RawUI.Set_WindowPosition($WindowPosition)
}
}