Created
May 21, 2020 20:28
-
-
Save tommymaynard/96f2a32c22290f8a199c460643b373ce to your computer and use it in GitHub Desktop.
TMConsole Module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| TechNet Contribution: TMConsole Module | |
| Previous link: https://gallery.technet.microsoft.com/TMConsole-Module-Clear-487eff0e | |
| Downloaded: 427 times (as of 05/21/2020) | |
| The TMConsole module is a basic, two function module. Although the concept is simple and the code is minimal, the impact | |
| of these two functions has been great. The Clear-TMConsole function, "clears" the console by shifting the the top of the | |
| console window out of view. Previously entered information can be viewed by scrolling up in the console. The New-TMConsole | |
| function, opens a new Windows PowerShell console from an existing PowerShell console and provides the user an option to | |
| close the current console. The default option will leave the current console open. | |
| Here are the aliases I use for the functions in this module. | |
| Set-Alias -Name nc -Value New-TMConsole | |
| Set-Alias -Name clx -Value Clear-TMConsole | |
| Set-Alias -Name cc -Value Clear-TMConsole | |
| Read more about the development of this module at the two links below. | |
| Clx was First Introduced: http://tommymaynard.com/ql-clear-host-without-clearing-the-host/ | |
| Combined clx into Module: http://tommymaynard.com/ql-clear-host-without-clearing-the-host-part-2/ | |
| Tommy Maynard (tommymaynard) | |
| Function Clear-TMConsole (inside module) updated: 11/4/2014 | |
| #> | |
| #Version 1.0 | |
| Function Clear-TMConsole { | |
| ##### ** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK ** | |
| <# | |
| .SYNOPSIS | |
| "Clears" the console by shifting the the top of the console window. | |
| .DESCRIPTION | |
| "Clears" the console by shifting the the top of the console window. Previously entered information can be viewed by scrolling up in the console. | |
| .PARAMETER SaveRows | |
| This paramenter allows the specified number of rows to be left as viewable. | |
| .EXAMPLE | |
| Clear-TMConsole | |
| This example will "clear" the console. | |
| .EXAMPLE | |
| Clear-TMConsole -SaveRows 5 | |
| This example will "clear" the console but leave the last 5 rows viewable. | |
| .NOTES | |
| NAME: Clear-TMConsole | |
| AUTHOR: Tommy Maynard | |
| WEBSITE: http://tommymaynard.com | |
| LASTEDIT: 10/31/2014 | |
| VERSION 1.0 | |
| VERSION 1.1: | |
| Added protection against requesting too many saverows. | |
| VERSION 1.2: | |
| Handled -SaveRows 0. | |
| Cast $SaveRows as int (it was string - no idea). | |
| Handled -SaveRows negative number. | |
| #> | |
| [CmdletBinding()] | |
| Param( | |
| [Parameter(Position=0)] | |
| [int]$SaveRows | |
| ) | |
| Begin { | |
| } # End Begin. | |
| Process { | |
| If ($SaveRows -gt 0) { | |
| Write-Verbose -Message '"Clearing" the console and saving lines.' | |
| $Left = ([System.Console]::CursorTop - $SaveRows) - 1 | |
| If ($Left -gt 0) { | |
| [System.Console]::SetWindowPosition(0,$Left) | |
| } Else { | |
| Write-Warning -Message 'Requested too many saverows.' | |
| } | |
| } ElseIf ($SaveRows -lt 0) { | |
| Write-Warning -Message 'Requested negative-valued saverows.' | |
| } Else { | |
| Write-Verbose -Message '"Clearing" the console.' | |
| [System.Console]::SetWindowPosition(0,[System.Console]::CursorTop) | |
| } | |
| } # End Process. | |
| } # End Function. | |
| Function New-TMConsole { | |
| ##### ** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK ** | |
| <# | |
| .SYNOPSIS | |
| Opens a new Windows PowerShell console from an existing PowerShell console. | |
| .DESCRIPTION | |
| Opens a new Windows PowerShell console from an existing PowerShell console and provides the user an option to close the current console. The default option will leave the current console open. | |
| .PARAMETER Continue | |
| This parameter is mandatory and requires a 'y' or 'yes' in order to run. This was included so that users wouldn't accidently run the function. | |
| .PARAMETER Keep | |
| This parameter allows the user to close the current PowerShell console when the new one is opened. | |
| .EXAMPLE | |
| New-TMConsole -Continue y (-or- New-TMConsole y) | |
| This example will open a new PowerShell console and leave the current console open. | |
| .EXAMPLE | |
| New-TMConsole -Continue y -Keep N (-or- New-TMConsole y n) | |
| This example will open a new PowerShell console and close the current console open. | |
| .NOTES | |
| NAME: New-TMConsole | |
| AUTHOR: Tommy Maynard | |
| WEBSITE: http://tommymaynard.com | |
| LASTEDIT: 10/23/2014 | |
| VERSION 1.0 | |
| VERSION 1.1: | |
| Remove all parts from function that created aliases. | |
| #> | |
| [CmdletBinding()] | |
| Param( | |
| [Parameter(Mandatory=$True,Position=0)] | |
| [string]$Continue, | |
| [Parameter(Position=1)] | |
| [string]$Keep | |
| ) | |
| Begin { | |
| } # End Begin. | |
| Process { | |
| ##### Opens (and possibly closes) new console window. | |
| Function Start-TMPowerShell($Exit) { | |
| Write-Verbose -Message 'Opening new console.' | |
| Start-Process -FilePath powershell.exe | |
| If ($Exit -eq 'y' -or $Exit -eq 'yes') { | |
| Write-Verbose -Message 'Closing current console.' | |
| Stop-Process -Id $PID | |
| } | |
| } | |
| ##### Check -Continue and -Keep parameters. | |
| If ($Continue -eq 'y' -or $Continue -eq 'yes') { | |
| Write-Verbose -Message 'User selected to continue opening a new console.' | |
| If ($Keep -eq 'n' -or $Keep -eq 'no') { | |
| Start-TMPowerShell -Exit 'yes' | |
| } Else { | |
| Start-TMPowerShell | |
| } | |
| } ElseIf ($Continue -ne 'n' -or $Continue -ne 'no') { | |
| Write-Verbose -Message 'INFO: User declined opening new console.' | |
| Write-Verbose -Message 'INFO: Rerun the function and enter y or yes to open a new console.' | |
| } | |
| } # End Process. | |
| } # End Function. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment