Rob van der Woude's Scripting Pages
Powered by GeSHi

PowerShell Code Snippets

Command Line

  1. Suppress Standard Output
  2. Suppress Standard Output (alternative)
  3. Suppress Standard Error
  4. Suppress Standard Error (alternative)
  5. Suppress all output
  6. Suppress all output (alternative)
  7. Suppress a function's result
  8. Suppress a function's result (alternative)

1. Suppress Standard Output

  1. <em>some-command</em> | Out-Null

 

2. Suppress Standard Output (alternative)

  1. some-command > $null

 

3. Suppress Standard Error

  1. Some-CmdLet -ErrorAction SilentlyContinue

 

4. Suppress Standard Error (alternative)

  1. some-command 2> $null

 

5. Suppress all output

  1. <em>Some-CmdLet</em> -ErrorAction SilentlyContinue | Out-Null

 

6. Suppress all output (alternative)

  1. some-command > $null 2>&1

 

7. Suppress a function's result

  1. [void] SomeFunction( )

 

8. Suppress a function's result (alternative)

  1. $dummy = SomeFunction( )

 

GUI

  1. Use System.Windows.Forms and System.Drawing to create GUIs
  2. Embed Base64 encoded icon

1. Use System.Windows.Forms and System.Drawing to create GUIs

  1. Add-Type -AssemblyName System.Windows.Forms
  2. Add-Type -AssemblyName System.Drawing
  3.  
  4. $form      = New-Object System.Windows.Forms.Form
  5. $form.Text = 'PowerShell GUI Demo'
  6. $form.Size = '400,240'
  7.  
  8. $label1          = New-Object System.Windows.Forms.Label
  9. $label1.Text     = 'Arial Arial Arial Arial Arial Arial Arial Arial Arial Arial Arial Arial Arial Arial Arial Arial Arial'
  10. $label1.Font     = New-Object System.Drawing.Font( 'Arial', 10 ) # This is the "official" way to specify a font
  11. $label1.Size     = New-Object System.Drawing.Size( 200, 40 )     # This is the "official" way to specify a size
  12. $label1.Location = New-Object System.Drawing.Point( 10, 25 )     # This is the "official" way to specify a location
  13. $form.Controls.Add( $label1 )
  14.  
  15. $label2          = New-Object System.Windows.Forms.Label
  16. $label2.Text     = 'Courier Courier Courier Courier Courier Courier Courier Courier Courier Courier Courier Courier Courier'
  17. $label2.Font     = 'Courier New,10' # This is an alternative way to specify a font
  18. $label2.Size     = '200, 40'        # This is an alternative way to specify a size
  19. $label2.Location = '10, 75'         # This is an alternative way to specify a location
  20. $form.Controls.Add( $label2 )
  21.  
  22. $buttonOK              = New-Object System.Windows.Forms.Button
  23. $buttonOK.Text         = 'OK'
  24. $buttonOK.DialogResult = 'OK' # This should usually not be required
  25. $buttonOK.Location     = '10, 150'
  26. $form.Controls.Add( $buttonOK )
  27.  
  28. $buttonCancel          = New-Object System.Windows.Forms.Button
  29. $buttonCancel.Text     = 'Cancel'
  30. $buttonCancel.Location = '120, 150'
  31. $form.Controls.Add( $buttonCancel )
  32.  
  33. $form.AcceptButton = $buttonOK # This should usually suffice to make the dialog return OK when OK is clicked
  34. $form.CancelButton = $buttonCancel
  35.  
  36. # Without [void] PowerShell will write the ShowDialog result (caption of the button clicked) to screen
  37. [void] $form.ShowDialog( )
  38.  
  39. # This is another way to get the form's ShowDialog result
  40. Write-Host $form.DialogResult

 

2. Embed Base64 encoded icon

  1. Add-Type -AssemblyName System.Windows.Forms
  2. Add-Type -AssemblyName System.Drawing
  3. # Base64 encoded icon
  4. $b64convicon  = "AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAAAAAAAAA"
  5. $b64convicon += "AAAAAAAAAAAAAAAAAAAEAgQAhIKEAPz+/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  6. $b64convicon += "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  7. $b64convicon += "AAAAAAAAAAAAAAAAAAAAEiEAAAICAAEgAgEAAgEgAgAAAgARACECAAAAACAAAgIA"
  8. $b64convicon += "AAEAIAACAgAAAgEQACEBIAIAAgABIAASIQACAAIAAAAAAAAAAAAAAAAAAAAAAAAA"
  9. $b64convicon += "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  10. $b64convicon += "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  11. $form = New-Object System.Windows.Forms.Form
  12. # Use Base64 encoded icon in code, by Kenny Baldwin
  13. # https://foxdeploy.com/2013/10/23/creating-a-gui-natively-for-your-powershell-tools-using-net-methods/#comments
  14. $form.Icon = ( [System.Drawing.Icon]( New-Object System.Drawing.Icon( ( New-Object System.IO.MemoryStream( ( $$ = [System.Convert]::FromBase64String( $b64convicon ) ), 0, $$.Length ) ) ) ) )
  15. $form.ShowDialog( )

 


page last uploaded: 2019-03-26, 14:27