PS: GUI Commands

5 minute read

Description:

Here is a list of random commands I use in Powershell that will probably end up being moved around later.

To Resolve:

To send key presses:

1
2
   $wshell = new-object -com wscript.shell
   $wshell.SendKeys("{CAPSLOCK}")

To create an input box:

1
2
   [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
   $data = [Microsoft.VisualBasic.Interaction]::InputBox('hi user give me your data', 'inputboxtitle')

To create a pop up box:

1
2
   [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
   [Microsoft.VisualBasic.Interaction]::MsgBox("tada","OKOnly,SystemModal,Information","msgboxtitle")

To create a GUI for Powershell scripts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
   [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
   [xml]$XAML = @'
   <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         Title="MainWindow" Height="350" Width="300" MinHeight="350" MinWidth="300" ResizeMode="CanResizeWithGrip">
      <Grid>
         <TextBox Name="textBox" Height="25" Margin="10,10,125,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
         <Button Name="button" Content="Button" Height="25" Margin="0,10,10,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="110"/>
         <DataGrid Name="dataGrid" Margin="10,40,10,10"/>
      </Grid>
   </Window>
   '@

   $reader=(New-Object System.Xml.XmlNodeReader $xaml) 
   try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
   catch{Write-Host "Unable to load Windows.Markup.XamlReader."; break}
   $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}

   $button.Add_Click({
      $textBox.Text = "OK!"
   })

   $Form.ShowDialog() | Out-Null

To Pause A Script:

1
2
3
4
5
   Function Pause-Script
   {
   Write-Output -InputObject "Press any key to continue…"
   [void]$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
   }

To Prompt For A Choice:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   $Title = "Continue"
   $Info = "Would you like to continue?"
   $Options = [System.Management.Automation.Host.ChoiceDescription[]] @("&Yes", "&No")
   [int]$DefaultChoice = 0
   $Opt = $host.UI.PromptForChoice($Title , $Info, $Options, $DefaultChoice)
   switch ($Opt)
   {
   0
   {
   Write-Verbose -Message "Yes"
   }
   1
   {
   Write-Verbose -Message "No"
   }
   }

To Hide A Window (For Form Launching Scripts):

1
2
3
4
5
6
7
   Function Initialize-Window
   {
   $t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
   Add-Type -Name Win -Member $t -Namespace native
   [native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
   }
   Initialize-Window

To Lock The Computer Screen:

1
   cmd /c "%windir%\system32\rundll32.exe user32.dll,LockWorkStation"

To Turn Off Monitors:

1
   powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0×0112,0xF170,2)

To Create FullScreen PopUps Transparent and Not:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
   Function Show-MessageFullScreenTransparent ( $message )
   {
   Add-Type -AssemblyName System.Windows.Forms
   $Screens = [system.windows.forms.screen]::AllScreens
   $i = 0;
   foreach ($Screen in $Screens)
   {
   $i++;
   if ( $Screen.Primary )
   {
   $w = $Screen.Bounds.Width #$Screen.WorkingArea.Width
   $h = $Screen.Bounds.Height #$Screen.WorkingArea.Width
   }
   }
   $objForm = New-Object System.Windows.Forms.Form
   $objForm.StartPosition = "CenterScreen"
   $objForm.Size = New-Object System.Drawing.Size($w, $h)
   $font = New-Object System.Drawing.Font("arial", 22, [System.Drawing.Fontstyle]::Regular)
   $objForm.Font = $font
   $text = New-Object System.Windows.Forms.Label
   $text.Text = "$message"
   $text.Size = New-Object System.Drawing.Size($w, $h)
   $text.TextAlign = "MiddleCenter"
   $objForm.Controls.Add($text)
   $objForm.Autosize = $true
   $objForm.MinimizeBox = $false
   $objForm.MaximizeBox = $false
   $objForm.ShowIcon = $false
   $objForm.TransparencyKey = "#000000"
   $objForm.BackColor = "#000000"
   $objForm.ForeColor = "#00FF00"
   $objForm.SizeGripStyle = "Hide"
   $objForm.FormBorderStyle = "None"
   $objForm.Show()
   Start-Sleep -Seconds 1
   $text = New-Object System.Windows.Forms.Label
   $text.Text = "please"
   $text.Size = New-Object System.Drawing.Size($w, $h)
   $text.TextAlign = "MiddleCenter"
   $objForm.Controls.Add($text)
   $objForm.Close()
   }
   Function Show-MessageFullScreenWithBackground ( $message )
   {
   Add-Type -AssemblyName System.Windows.Forms
   $Screens = [system.windows.forms.screen]::AllScreens
   $i = 0;
   foreach ($Screen in $Screens)
   {
   $i++;
   if ( $Screen.Primary )
   {
   $w = $Screen.Bounds.Width #$Screen.WorkingArea.Width if you don't want to go full fullscreen
   $h = $Screen.Bounds.Height #$Screen.WorkingArea.Height if you don't want to go full fullscreen
   }
   }
   $objForm = New-Object System.Windows.Forms.Form
   $objForm.StartPosition = "CenterScreen"
   $objForm.Size = New-Object System.Drawing.Size($w, $h)
   $font = New-Object System.Drawing.Font("arial", 22, [System.Drawing.Fontstyle]::Regular)
   $objForm.Font = $font
   $text = New-Object System.Windows.Forms.Label
   $text.Text = "$message"
   $text.Size = New-Object System.Drawing.Size($w, $h)
   $text.TextAlign = "MiddleCenter"
   $objForm.Controls.Add($text)
   $objForm.Autosize = $true
   $objForm.MinimizeBox = $false
   $objForm.MaximizeBox = $false
   $objForm.ShowIcon = $false
   $objForm.BackColor = "#000000"
   $objForm.ForeColor = "#00FF00"
   $objForm.SizeGripStyle = "Hide"
   $objForm.FormBorderStyle = "None"
   $objForm.Show()
   Start-Sleep -Seconds 1
   $text = New-Object System.Windows.Forms.Label
   $text.Text = "please"
   $text.Size = New-Object System.Drawing.Size($w, $h)
   $text.TextAlign = "MiddleCenter"
   $objForm.Controls.Add($text)
   $objForm.Close()
   }
   Show-MessageFullScreenTransparent -Message "Stop"
   Show-MessageFullScreenWithBackground "No really, stop!!!!"
   Show-MessageFullScreenTransparent -Message "Let's Get Started"

To Display A Picture:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   $Picture = 'C:\temp\pic.jpg'
   [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
   $file = (get-item $Picture)
   $img = [System.Drawing.Image]::Fromfile($file);
   [System.Windows.Forms.Application]::EnableVisualStyles();
   $form = new-object Windows.Forms.Form
   $form.Text = "Image Viewer"
   $form.Width = $img.Size.Width;
   $form.Height =  $img.Size.Height;
   $pictureBox = new-object Windows.Forms.PictureBox
   $pictureBox.Width =  $img.Size.Width;
   $pictureBox.Height =  $img.Size.Height;
   $pictureBox.Image = $img;
   $form.controls.add($pictureBox)
   $form.Add_Shown( { $form.Activate() } )
   $form.ShowDialog()

To Encode / Decode The Picture In a Script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   $Picture = 'C:\temp\pic.jpg'

   # Convert-FileToBase64
   $EncodedFile = [convert]::ToBase64String( (get-content -Path $Picture -Encoding Byte) )
   $EncodedFile = $EncodedFile -replace '.{64}', "$&`r`n"

   # Embed the script in the file
   $Base64 = @'
   Super long String from above....
   '@
   $Content = [System.Convert]::FromBase64String($Base64)
   Set-Content -Path $env:temp\file.jpg -Value $Content -Encoding Byte

   # Delete the temp file
   Remove-Item $env:temp\file.jpg

To Use Jobs To Show Progress Bars:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   # Start with a collection of items to show progress on while we loop through
   $Nums = 1..10

   # Start a job that is NOT set to OK until someone clicks 'OK'
   $Null = Start-Job -Name 'Test' -Scriptblock {
      Add-Type -Assemblyname System.Windows.Forms
      [System.Windows.Forms.Messagebox]::Show('Cancel?', 'Write-Progress Test', 0)
   }

   # Initialize counter outside of the loop
   $Index = 0

   # Go Through Each Item in Items
   Foreach ($Num In $Nums)
   {
      # Increment counters until we run out of items
      $Index++
      # Just a display to show the progress
      Write-Progress -Activity 'Test' -Percentcomplete ($Index / $Nums.Count * 100)
      # Referencing the above job... Only if the user presses 'OK', break out of the script
      If ((Receive-Job -Name 'Test').Value -Eq 'Ok')
      {
         Break
      }
      # Otherwise, sleep for one second and go to the next item until all items are processed.
      Start-Sleep 1
   }
   # End the script since all the items were processed and the user never hit 'OK'
   Remove-Job -Name 'Test'
   # Optional - Show what number they stopped at if they hit it early on or just waited until the end. 
   Write-Output "You Stopped At $Num"

Comments