Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun – A Popup Alternative

Posted on September 12, 2014September 10, 2014

windowpane-thumbnail In the past I've written and presented about different ways to add graphical elements to your PowerShell scripts that don't rely on Windows Forms or WPF. There's nothing wrong with those techniques, but they certainly require some expertise and depending on your situation may be overkill. So let's have some fun today and see how you can display a popup message in a graphical form, using something you already know how to use.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

It may not be sexy, but how about using Notepad. Here's how. First, you need some content to display. Using a here string is good approach as it makes it easy to create a multi-line block of text.

$text=@"
This part of the script has completed.
Close Notepad to continue.
Thank you.
"@

With a here string you could also insert variables.

$var = "Foo"
$text=@"
$(Get-Date)
Processing has finished on $($env:COMPUTERNAME)

The final value of var is $var
"@

Next, create a temporary file with the text.

$text | Out-File -FilePath d:\temp\mytemp.txt -Encoding ascii

To display the message all you need to do is run Notepad.

notepad d:\temp\mytemp.txt

This will return your PowerShell prompt but you want to make the popup more integral to your script, in which case I'd suggest using Start-Process.

Start-Process notepad -ArgumentList d:\temp\mytemp.txt -Wait -noNewWindow

The major advantage is that when you close Notepad your script continues so you can then delete the temp file. While we're mentioning the temp file, if you tried my demo you'll notice that the file name is displayed in the title bar. There's no requirement that your file have an extension.

[string]$Title = "Script Complete"
$tmpFile = Join-Path -Path $env:temp -ChildPath $Title
$text | Out-File -FilePath $tmpFile -Encoding ascii
Start-Process notepad -ArgumentList $tmpFile -Wait -noNewWindow

Notice my use of Join-Path to create a file name. I suggest you use this cmdlet instead of trying to concatenate pieces together. But now I get something a little nicer.

notepad-popup

With Notepad you could save the file elsewhere or even print it. I even created a re-usable function for you.

Function New-NotepadPopup {

<#
.Synopsis
Display a popup message in Notepad
.Description
This command displays a text message in a Notepad window.
.Parameter Text
The text to be displayed in Notepad. Using a here string is recommended.
.Parameter Title
This will be used for the file name. You don't need to include a file extension.
.Parameter Wait
Force the PowerShell to wait until Notepad is closed. 
.Example
PS C:\> $text=@"
This part of the script has completed.
Close Notepad to continue.
Thank you.
"@
PS C:\> New-NotepadPopup $Text -title "Finished" -wait
.Notes
Last Updated: September 10, 2014
Version     : 1

.Link
Start-Process
#>

[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter text to be displayed")]
[ValidateNotNullorEmpty()]
[string]$Text,
[Parameter(Position=1)]
[ValidateNotNullorEmpty()]
[ValidatePattern({^\w+$})]
[string]$Title = "Notice",
[switch]$Wait
)

Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"  

#create a temp filename
$tmpFile = Join-Path -Path $env:temp -ChildPath $Title

#create the temp file
Write-Verbose "Creating $tmpFile"
$text | Out-File -FilePath $tmpFile -Encoding ascii

#launch notepad and wait
Write-Verbose "Displaying Notepad"

#hashtable of values to splat to Start-Process
$paramHash = @{
 Filepath = join-path -path $env:windir -childpath notepad.exe
 argumentList = $tmpFile
 NoNewWindow = $True
}

If ($Wait) {
    $paramHash.Add("Wait",$True)
}

Start-Process @paramHash

#if not waiting sleep for a few seconds to give Notepad a chance to display the file
if (-Not $Wait) {
    Start-Sleep -Seconds 2
}

#delete temp file
Write-Verbose "Deleting $tmpFile"
Remove-Item $tmpFile

Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"


} #end function

This function will automatically delete the temp file for you. If you have another text file viewer that you can launch from the command line you could use that instead of Notepad. As I mentioned at the beginning this is hardly exciting PowerShell but I hope you picked up something new and useful. Have a great weekend.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d