As adminstrator you are not only responsible for assigning group policies or playing the cop who always watch what users do or not. A major part of your duties is to make sure your users can work as comfortably as possible. This is what this blog post is about. I will show you a code sample which creates a graphical desktop shortcut icon. With this dektop icon users can get infos about their computer and – if needed – a link to the IT ticket system.
The Objective
The objective is to make this screen happen when a users klicks on a shortcut on his desktop.
All this contributes to the fact that the user can easily retrieve information about his computer and contact the helpdesk with this information.
Which brings me to the next part. It’s about the code behind this pop-up window.
The Code (WindowsForms)
You can create a pop-up window in PowerShell in two main ways: VB and WindowsForms. I decided to go with Windows Forms so the code below is written with Forms.
Important Notes
Code tested in PowerShell 5.1 and PowerShell 7.
You certainly want to customize the sample code. I have prepared some useful tips and a reference to the lines which should be adapted.
Line 9: edit head
Line 12: icon edit top left (if icon is missing, default icon is used)
Line 29,30: edit link text and hyperlink to help system
And finally, here is the code.
Copy it into your favourite editor such as PowerShell ISE or VS Code.
$ErrorActionPreference = "SilentlyContinue" Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $font = New-Object System.Drawing.Font("Arial", 9) $form = New-Object System.Windows.Forms.Form $form.Text = 'Computer-Info' $form.Size = New-Object System.Drawing.Size(325,225) $form.StartPosition = 'CenterScreen' $form.Icon = 'C:\users\patri\OneDrive\Bilder\favicon.ico' $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Point(120,150) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = 'OK' $OKButton.TextAlign = 'MiddleCenter' $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.AcceptButton = $OKButton $form.Controls.Add($OKButton) $LinkLabel = New-Object System.Windows.Forms.LinkLabel $LinkLabel.Location = New-Object System.Drawing.Point(81,110) $LinkLabel.Size = New-Object System.Drawing.Size(280,20) $LinkLabel.Font = $font $LinkLabel.LinkColor = "BLUE" $LinkLabel.ActiveLinkColor = "RED" $LinkLabel.Text = "Ticketsystem" $LinkLabel.add_Click({[system.Diagnostics.Process]::start("https://sid-500.com")}) $Form.Controls.Add($LinkLabel) $hostn = New-Object System.Windows.Forms.Label $hostn.Location = New-Object System.Drawing.Point(10,10) $hostn.Size = New-Object System.Drawing.Size(280,20) $hostn.Font = $font $hostn.Text = "Computer: $(hostname)" $form.Controls.Add($hostn) $os = New-Object System.Windows.Forms.Label $os.Location = New-Object System.Drawing.Point(10,90) $os.Size = New-Object System.Drawing.Size(280,20) $os.Font = $font $os.Text = "OS: $((Get-CimInstance win32_operatingsystem).Caption.Trimstart('Microsoft '))" $form.Controls.Add($os) $mac = New-Object System.Windows.Forms.Label $mac.Location = New-Object System.Drawing.Point(10,50) $mac.Size = New-Object System.Drawing.Size(280,20) $mac.Font = $font $mac.Text = "MAC: $((Get-NetAdapter | Where-Object Status -EQ 'up').MacAddress)" $form.Controls.Add($mac) $user = New-Object System.Windows.Forms.Label $user.Location = New-Object System.Drawing.Point(10,30) $user.Size = New-Object System.Drawing.Size(280,20) $user.Font = $font $user.Text = "User: $env:username" $form.Controls.Add($user) $ip = New-Object System.Windows.Forms.Label $ip.Location = New-Object System.Drawing.Point(10,70) $ip.Size = New-Object System.Drawing.Size(280,20) $ip.Font = $font $ip.Text = "IP: $((Get-NetAdapter | Where-Object Status -EQ 'up' | Get-NetIPAddress -AddressFamily IPv4).IPAddress)" $form.Controls.Add($ip) $help = New-Object System.Windows.Forms.Label $help.Location = New-Object System.Drawing.Point(10,110) $help.Font = $font $help.Size = New-Object System.Drawing.Size(280,20) $help.Text = "Help:" $form.Controls.Add($help) $form.Topmost = $true $form.ShowDialog()
Now it’s your turn to distribute the code as a link to your workstations via GPOs.
See also
https://docs.microsoft.com/en-us/dotnet/framework/winforms/
Categories: PowerShell, Windows 10
When using this on VPN and with Vmware Workstation installed you get a lot of ‘false’ information. How to exclude these?
LikeLike
Hello Patrick, how are you? Please do you help me. I can`t create GPO for your upstair script. Please do you help me. I create GPO in my domain, but when I click to script, nothing doesn`t happen.
LikeLike
Hi, haven’t tried it so far with GPOs.
LikeLike
Hallo Patrick, how are you today? Your script is beautiful. I have 2 questions for you. First is how can I put on desktop (using shortcut) and Secon question is how Can I distribute via GPO in whole domain
LikeLike
Hi,
Configuring GPOs is out of scope for this blog post. See more about Group Policy preferences in my other articles.
Best,
P
LikeLiked by 1 person