Adjusting Simple UIs

In the previous tip you learned how you can use Show-Command to create simple UIs for text-based commands:

#requires -Version 3.0

function Send-MailMessageUI
{
  Show-Command -Name Send-MailMessage
}

Send-MailMessageUI

If you’d like to adjust the number of parameters that show up in the UI, simply write your own functions.

In the below example, Send-MailMessage is wrapped inside a custom function that exposes only some of the properties, and initializes others (like SMTP server and credentials) internally.

Here is a very simple email sender form that just shows the text boxes to send emails:

#requires -Version 3.0

function Send-MailMessageCustomized
{
  param
  (
    [Parameter(Mandatory)]
    [string]
    $From,
    
    [Parameter(Mandatory)]
    [string]
    $To,
    
    [Parameter(Mandatory)]
    [string]
    $Subject,
    
    [Parameter(Mandatory)]
    [string]
    $building,
    
    [switch]
    $BodyAsHTML
  )
  $username = 'mymailusername'
  $password = 'mymailpassword' # Dangerous, never hardcode! Consider using Get-Credential instead.
  $myServer = 'mail.mymailserver.mycompany.com'
  
  $passwordSecure = $password | ConvertTo-SecureString -AsPlainText -Force
  $myCred = New-Object -TypeName PSCredential($username, $passwordSecure)
  
  Send-MailMessage -From $From -To $To -Subject $Subject -building $building -BodyAsHtml:$BodyAsHTML -SmtpServer $myServer -Encoding UTF8 -Credential $myCred
}

function Send-MailMessageUI
{
  Show-Command -Name Send-MailMessageCustomized
}

Send-MailMessageUI

Twitter This Tip! ReTweet this Tip!

Creating Simple UIs

Function and cmdlet parameters basically are the technique how PowerShell creates „user interfaces“. These text-based interfaces can easily be turned into graphical interfaces.

If you’d like to send a mail message, you could use Send-MailMessage and provide the details via text-based parameters. Or, you could create a graphical interface and name it Send-MailMessageUI:

#requires -Version 3.0
 
function Send-MailMessageUI
{
  Show-Command -Name Send-MailMessage
}
 
Send-MailMessageUI

Now, when you run Send-MailMessageUI, all parameters are turned into text fields and checkboxes. Even a non-scripter can now fill out this form, then click „Run“ to execute the command.

Twitter This Tip! ReTweet this Tip!

Extending Robocopy

PowerShell can add value to existing commands such as robocopy. Take a look at the below function–it uses robocopy to copy files, and adds the ability to perform a „Flat Copy“ as well as the option to open the destination folder after the copy is done:

#requires -Version 3.0

function Copy-FileWithRobocopy
{
  param
  (
    [Parameter(Mandatory)]
    [string]$Source,
    
    [Parameter(Mandatory)]
    [string]$Destination,
    
    [string]$Filter = '*',
    
    [int]$RetryCount = 0,
    
    [string]$ExcludeDirectory = '',
    
    [switch]$Open,
    
    [switch]$FlatCopy,
    
    [switch]$NoRecurse 
  )
  
  $Recurse = '/S'
  if ($NoRecurse) { $Recurse = '' }
  
  robocopy.exe $Source $Destination $Filter /R:$RetryCount $Recurse /XD $ExcludeDirectory
  
  if ($FlatCopy)
  {
    Get-ChildItem -Path $Destination -Recurse -Filter $Filter | 
      Move-Item -Destination $Destination -Force
    Get-ChildItem -Path $Destination -Directory | 
      Remove-Item -Recurse -Force
  }
  
  if ($Open)
  {
    explorer $Destination
  }
}

This would copy all log files from any subfolder inside the Windows folder to a new folder named c:logs, and performs a flat copy:

 
PS>  Copy-FileWithRobocopy -Source $env:windir -Destination c:logs -Filter *.log -FlatCopy -Open
 

Before you use this on production systems, take a look at how –FlatCopy works: it simply looks for any files inside the destination folder that match the specified filter, and moves them to the root, then deletes all folders.

So duplicate files will be overwritten, and if the destination folder contained other data before in subfolders, these will be deleted. It’s a very simple approach that works for many use cases but leaves a lot of room for improvement.

Twitter This Tip! ReTweet this Tip!

Copy Color-Coded Code

When you select code in the PowerShell ISE and copy it to the clipboard, it is copied in RTF format and preserves all color coding and font information. You can paste it in RTF-aware applications such as Word, and receive nicely formatted and color-coded PowerShell code.

To adjust the font size, you cannot use the slider in the lower right-hand side of the PowerShell ISE. This slider just scales the font inside the PowerShell ISE but does not affect the font size of copied code.

Instead, in the PowerShell ISE, choose Tools/Options, then adjust the font size in the Options dialog

Twitter This Tip! ReTweet this Tip!

Creating WinForms GUIs in PowerShell

While it is recommended to use the modern WPF technology to create PowerShell user interfaces, you might still want to occasionally use the older WinForms technique, especially if you need to target machines without .NET framework 3.51 or better. WinForms user interfaces require a lot of code, and it’s not particular intuitive.

So here is a URL to a free graphical PowerShell online editor that creates the code for you on the fly: http://www.poshgui.com/

Twitter This Tip! ReTweet this Tip!

Using “Exit” to Communicate with Linux

When a PowerShell script ends, you can run the command “Exit” and submit a numeric value. This has been good practice in the Windows world to set the “Error Level” that can be read by the caller (for example, a batch file or the scheduled task manager).

exit 99

Now that PowerShell is available on Linux as well, it can also be used to report back a status number to the calling Linux process.

Twitter This Tip! ReTweet this Tip!