All posts by Thiyagu

Reading XML Files With PowerShell

Handling XML with PowerShell is very simple. It converts XML elements to properties on .NET objects without the need to write any parsing code. So it is very easy to use XML with PowerShell. Here we are going to discuss how to read the XML file values in different ways.

You can also read  How to add values to the string array from xml using Powershell and  How to write data to an xml file using Powershell 

A Quick Example

XML File : MyXM.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<EmpDetails>
	<Person>
		<Id>E00023</Id>
		<mailId>Aadharsh@dotnet-helper.com</mailId>
		<empName>Aadharsh G.K</empName>		
	</Person>
	<Person>
		<Id>E00042</Id>
		<mailId>Rakshu@dotnet-helper.com</mailId>
		<empName>RakshaNivasini S.T</empName>		
	</Person>
</EmpDetails>

Method 1:

Once you’ve run Get-Content to read the raw text from the XML document and cast the output to type [XML], you now have a variable called $XmlDocument that contains the entire XML node tree which we can process.

STEP  #1: Assign the XML file location into the variable.

$XMLfile = ‘C:\donet-helpers\Demo\MyXM.xml’

STEP  #2: Read the xml file using get-content cmdlet and assign to variable. The [xml] casts the variable as an XML object.

[XML]$empDetails = Get-Content $XMLfile

STEP  #3: Looping the child node to get the value.

foreach($empDetail in $empDetails.EmpDetails.Person){ }

Final Code:

############################################################
#Project : Reading XML files with PowerShell
#Developer : Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 
#E-Mail : mail2thiyaguji@gmail.com 
############################################################
$XMLfile = 'C:\D_EMS Drive\Personal\LBLOG\testpath.xml' [XML]$empDetails = Get-Content $XMLfile foreach($empDetail in $empDetails.EmpDetails.Person){ Write-Host "Employee Id :" $empDetail.Id Write-Host "Employee mail Id :" $empDetail.mailId Write-Host "Employee Name :" $empDetail.empName Write-Host '' }

OUTPUT:

Method 2:

STEP  #1: Assign the XML file location into the variable.

$Path = “C:\donet-helpers\Demo\MyXM.xml”

STEP  #2: The below command saves the XML path to the AliasProperty node in the $XPath variable.

$XPath = “/EmpDetails/Person”

STEP  #3: The Select-Xml cmdlet lets you use XPath queries to search for text in XML strings and documents. Enter an XPath query, and use the Content, Path, or Xml parameter to specify the XML to be searched.

Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node

Final Code:

############################################################
#Project : Reading XML files with PowerShell
#Developer : Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 
#E-Mail : mail2thiyaguji@gmail.com 
############################################################
$Path = "C:\D_EMS Drive\Personal\LBLOG\testpath.xml" $XPath = "/EmpDetails/Person" Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node

 

What do you think?

I hope you have an idea of how to Reading XML Files With PowerShell. I would like to have feedback from the readers of my posts. Your valuable feedback, question, or comments about this article are always welcome.

Creating HTML report with CSS (Cascading Style Sheet) using Powershell

All Tech/non Tech peoples loves a nice HTML report for reviewing. Creating these type of reports in PowerShell is very easy and simple. These type of nice HTML reports can be generate with the help ConvertTo-HTML cmdlet. Converts Microsoft .NET Framework objects into HTML that can be displayed in a Web browser. The ConvertTo-HTML does not create an actual file, just the HTML code.This means that we can modify the HTML on the fly and save the file when finished. 

For this simple report i had used only few properties like -CssUri , -Postcontent and -PreContent from the ConvertTo-HTML cmdlet.

Pre/Post Content

Usually we will create table and generate content with the help of ConvertTo-HTML cmdlet. But if you want to insert additional HTML directives to put in before and after the main code. This can be achieve using “-PreContent” and “-PostContent” cmdlet.

-PreContent : Specifies text to add before the starting tag. By default, there is no text in that position

-PostContent : Specifies text to add after the closing tag. By default, there is no text in that position

CssUri

Specifies the Uniform Resource Identifier (URI) of the cascading style sheet (CSS) that is applied to the HTML file. The URI is included in a style sheet link in the output.

Final code:

Here i had wrote the required CSS code for the report and placed the below code in notepad and saved with the name C:\dotnet-helpers\design.css. The same CSS will be called during the report generation. So the style will be applied to the final HTML report.

body { background-color:#E5E4E2;
font-family:Monospace;
font-size:10pt; }
table,td, th { border:1px solid black;}
th { color:#00008B;
background-color:white; 
font-size: 12pt;}
table { margin-left:30px; }
h2 {
font-family:Tahoma;
color:#6D7B8D;
}
h1{color:#DC143C;}
h5{color:#DC143C;}

In the below script I’m adding style from an external CSS file with help of -CssUri parameter (-CssUri ‘C:\temp\design.css’) and generating the HTML report in the destination location. The CSS from the file will applied to the HTML element during the execution and generate the report as like below.

Get-Eventlog -List | Select @{Name="Logs";Expression = {$_.LogDisplayname}} , 
@{Name="Entries";Expression = {"{0:n0}" -f $_.entries.count}} |
#Converts Microsoft .NET Framework objects into HTML that can be displayed in a Web browser.
#the Title, Body, PreContent, and PostContent parameters of ConvertTo-Html to customize the output..
convertto-html -Title "Daily Log Report" -PreContent "<H1>Daily Event Log Report : $($env:COMPUTERNAME)</H1>" -PostContent "<H5><i>Copy right @ dotnet-helpers.com</i></H5>" -CssUri 'C:\dotnet-helpers\design.css' |
Set-Content C:\dotnet-helpers\DailyLogReport.htm

Output:

Move files to another directory Based on Filename with Powershell

Where this code required?

In many scenario, we will got the requirement for moving the specific files from the large number of files from one location to another location based some filter  on the file name. This code will helps with same situation. The below code will split the filename before “-” and compare the existing list “filterLists” to find 

STEP #1

Declare the souce and target path locations.

$srcpath = “C:\Powershelltest\Source”
$dstpath = “C:\Powershelltest\Destination”

STEP #2

Fetch all the child file list to make the loop to start the validation of each files.

$fileList = Get-ChildItem -Path $srcpath -Force -Recurse
foreach ($file in $fileList)
{
}

STEP #3

Second level of loop for validating whether filename is mathing with our input

foreach($filelist in $filterLists)
{
#Our Logic
}

Complete Code

############################################################
#Project : Move files to another directory by File name filter
#Developer : Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 
#E-Mail : mail2thiyaguji@gmail.com 
############################################################
#set Source and Destination folder location
$srcpath = "C:\Powershelltest\Source"
$dstpath = "C:\Powershelltest\Destination"
#Set the files name which need to move to destination folder
$filterLists = @("70022333","70022867")
#Get all the child file list with source folder
$fileList = Get-ChildItem -Path $srcpath -Force -Recurse
#loop the source folder files to find the match
foreach ($file in $fileList)
{
#checking the match with filterlist
foreach($filelist in $filterLists)
{
#$key = $file.BaseName.Substring(0,8)
#Spliting value before "-" for matching with filterlists value
$splitFileName = $file.BaseName.Substring(0, $file.BaseName.IndexOf('-'))
if ($splitFileName -in $filelist)
{
$fileName = $file.Name
Move-Item -Path $($file.FullName) -Destination $dstpath
}
}
}

Output

After Execution

What do you think?

I hope you have an idea of  Move files to another directory Based on Filename with Powershell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.

How to Find Out Specifications of Server Using PowerShell

When you are running a Windows server and you need to find out the specification of your servers  then this tutorial will helps you to achieve with the help of PowerShell script.

Step 1#: Open PowerShell with secific privileges.

Step 2#: Execute below script to get the specifications of the Servers

Get-wmiobject win32_computersystem
Get-WmiObject win32_processor

Explanation :

Get-wmiobject : Gets instances of WMI classes or information about the available classes.
win32_computersystem: The Win32_ComputerSystem WMI class represents a computer system running Windows.
win32_processor : Its WMI class represents a device that can interpret a sequence of instructions on a computer running on a Windows operating system.

OUTPUT:

What do you think?

I hope you have an idea of  How to Find Out Specifications of Server Using PowerShell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.

Upload files with Powershell GUI

In many scenarios, we will get the requirement to upload the reports, tracker from one location to another location , so i decided to write this post to explain how to upload files like excel, txt… with the help of Powershell GUI. Instead of thinking high level, here i had split the three simple task to achieve the file upload functionality in the Powershell.

  1. Create Simple PowerShell UI for browsing the location of files  with one textbox and two button type (upload & cancel) with help of system.Windows.Forms
  2. In Browse button click event, you can perform the browse with the specific file that need to be upload to destination folder using New-Object -TypeName System.Windows.Forms.OpenFileDialog. These make the selection of files or folders easy for users and prevent mistakes being made in file paths.
  3. In upload button click event, you can perform the Move action on the selected file to place on Target location.

When designing PowerShell scripts, I have often found it useful to incorporate a file or folder dialogue to select files or folders. These make the selection of files or folders easy for users and prevent mistakes being made in file paths.

Example:

####################################################### 
#Project      :    Upload the files using Powershell 
#Developer    :    Thiyagu S (dotnet-helpers.com)
#Tools        :    PowerShell 5.1.15063.1155 
#E-Mail       :    mail2thiyaguji@gmail.com 
####################################################### 
#Form class
$Forms = 'system.Windows.Forms.Form'
#Button class
$Button = 'system.Windows.Forms.Button'
#Getting the font details for applying the fields
$FontStyle = 'Microsoft Sans Serif,10,style=Bold'
$SysDrawPoint = 'System.Drawing.Point'
# setting InitialDirectory which open as intially during the click of browse button
$InitialDirectory = "C:\New folder"
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#The New-Object cmdlet creates an instance of a .NET Framework or COM object.
#Specifies the fully qualified name of the .NET Framework class. You cannot specify both the TypeName parameter and the ComObject parameter.
$TrackerFileUpload = New-Object -TypeName $Forms
$TrackerFileUpload.ClientSize = '400,114'
$TrackerFileUpload.text = 'dotnet-helpers.com - Please upload Daily Tracker'
$TrackerFileUpload.TopMost = $false
$txtFileName = New-Object -TypeName system.Windows.Forms.TextBox
$txtFileName.width = 280
$txtFileName.height = 20
$txtFileName.location = New-Object -TypeName $SysDrawPoint -ArgumentList (107,39)
$txtFileName.Font = $FontStyle
$btnFileBrowser = New-Object -TypeName $Button
$btnFileBrowser.BackColor = '#1a80b6'
$btnFileBrowser.text = 'Browse'
$btnFileBrowser.width = 96
$btnFileBrowser.height = 30
$btnFileBrowser.location = New-Object -TypeName $SysDrawPoint -ArgumentList (200,78)
$btnFileBrowser.Font = $FontStyle
$btnFileBrowser.ForeColor = '#ffffff'
$btnUpload = New-Object -TypeName $Button
$btnUpload.BackColor = '#00FF7F'
$btnUpload.text = 'Upload'
$btnUpload.width = 96
$btnUpload.height = 30
$btnUpload.location = New-Object -TypeName $SysDrawPoint -ArgumentList (291,78)
$btnUpload.Font = $FontStyle
$btnUpload.ForeColor = '#000000'
$lblFileName = New-Object -TypeName system.Windows.Forms.Label
$lblFileName.text = 'Choose File'
$lblFileName.AutoSize = $true
$lblFileName.width = 25
$lblFileName.height = 10
$lblFileName.location = New-Object -TypeName $SysDrawPoint -ArgumentList (20,40)
$lblFileName.Font = $FontStyle
#Adding the textbox,buttons to the forms for displaying
$TrackerFileUpload.controls.AddRange(@($txtFileName, $btnFileBrowser, $lblFileName, $btnUpload))
#Browse button click event
$btnFileBrowser.Add_Click({
Add-Type -AssemblyName System.windows.forms | Out-Null
#Creating an object for OpenFileDialog to a Form
$OpenDialog = New-Object -TypeName System.Windows.Forms.OpenFileDialog
#Initiat browse path can be set by using initialDirectory
$OpenDialog.initialDirectory = $initialDirectory
#Set filter only to upload Excel file
$OpenDialog.filter = '.xlsx (*(.xlsx)) | *.xlsx | *.txt'
$OpenDialog.ShowDialog() | Out-Null
$filePath = $OpenDialog.filename
#Assigining the file choosen path to the text box
$txtFileName.Text = $filePath 
$TrackerFileUpload.Refresh()
})
#Upload button click eventy
$btnUpload.Add_Click({
#Set the destination path
$destPath = 'C:\EMS\Destination'
#$txtFileName.Text = ""
Copy-Item -Path $txtFileName.Text -Destination $destPath
})
$null = $TrackerFileUpload.ShowDialog()
$txtFileName.Text = ""

Output:

What do you think?

I hope you have an idea of  How To upload the files with help of PowerShell GUI. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.

How To Create Progress Bars in PowerShell

Mostly No one have interest to wait for some time to complete the work/task/execution.But in certain situation, that’s is not always possible to completed the task without waiting for few seconds/Minutes. In Powershell if you wrote some automation and if sometimes it will to be take more time to execution due to gathering lot of information from then, reality, the script will take a time to complete.

In this scenario the script writer need to show some progress bar to indicate the user about the progress instead of blinking cursor at the delay. This can be achievable by using Write-Progress cmdlet which Displays a progress bar within a Powershell command window.

Syntax:

Write-Progress
[-Activity] <String>
[[-Status] <String>]
[[-Id] <Int32>]
[-PercentComplete <Int32>]
[-SecondsRemaining <Int32>]
[-CurrentOperation <String>]
[-ParentId <Int32>]
[-Completed]
[-SourceId <Int32>]
[<CommonParameters>]

Example:

############################################################
#Project : How To Create Progress Bars in PowerShell
#Developer : Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 
#E-Mail : mail2thiyaguji@gmail.com 
############################################################
$progressTimes = 20
$ioop_index = 0
for ($ioop_index=0;$ioop_index -lt $progressTimes; $ioop_index++) {
$completeStatus = ($ioop_index / $progressTimes) * 100
#Write-Progress: Displays a progress bar within a PowerShell command window.
#-Activity: Specifies the first line of text in the heading above the status bar. This text describes the activity whose progress is being reported.
#-Status: Specifies the second line of text in the heading above the status bar. This text describes current state of the activity.
#-PercentComplete: This command displays the progress of a For loop that counts from 1 to 20. 
Write-Progress -Activity 'in Progress' -Status "Did thing $ioop_index times" -PercentComplete $completeStatus -SecondsRemaining ($progressTimes-$ioop_index)
#Suspends the activity in a script or session for the specified period of time.
Start-Sleep 1
}

Output:

What do you think?

I hope you have an idea of  How To Create Progress Bars in PowerShell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.

Add text to Word Using PowerShell

For most of the automation and report we will prefer and use Excel , instead of excel her we look at how you can use Word with PowerShell to create a document.One of the first things that you need to do is ensure that you have Word installed on your computer. Once you have that verified, we can begin by creating a COM object connection to Word.Application, which will help us to begin the interacting with Word.

Running the below script that was just looking to create the document without showing anything, and making Visible property on the object to True. So after creating this object ($word) which will help to interacting with Word and its ready for our operations.

$word=new-object -ComObject “Word.Application”
$Word.Visible = $True

Before using edit on the word document before that, we need to add a document to our Word object and then select it so we can being adding text to it.

$doc = $word.documents.Add()
$myDoc = $word.Selection

Now we are ready to begin to edit and start writing in our Word document.Here i am going to generate the comments document from the PowerShell script, so you can achieve this using the TypeText() method, which is available on $myDoc object.

If we use of the TypeText method, the we need to understand the text will appear on the same line as the rest of the text. So we need to use the TypeParagraph method which use to start on a new line like break tag in HTML and then you use TypeText to begin writing on a brand-new line.

$myDoc.Style=”Strong”
$myDoc.TypeParagraph()
$myDoc.TypeText(“Hi All, “)
$myDoc.TypeParagraph()
$myDoc.TypeText(“This is Message from dotnet-helpers.com “)

Final Code:

####################################################### 
#Project : Writing in Word doc using Powershell 
#Developer : Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 
#E-Mail : mail2thiyaguji@gmail.com 
####################################################### 
$savepath="C:\dotnet-helpers\ReviewCommends.docx" 
$word=new-object -ComObject "Word.Application"
$Word.Visible = $True
$doc = $word.documents.Add() 
$myDoc = $word.Selection
$myDoc.Style="Strong" 
$myDoc.Font.Bold = 1
$myDoc.TypeParagraph()
$myDoc.TypeText("Hi All, ") 
$myDoc.TypeParagraph()
$myDoc.TypeText("This is Message from dotnet-helpers.com ")
$myDoc.Style="Normal" 
$myDoc.Font.Italic = 1
$myDoc.TypeParagraph() 
$myDoc.TypeText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes,nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu,
pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel,aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae") 
$myDoc.TypeParagraph() 
$myDoc.TypeParagraph() 
$myDoc.TypeText("Thanks") 
$myDoc.TypeParagraph() 
$myDoc.Style="Strong" 
$myDoc.TypeText("Thiyagu S") 
$myDoc.TypeParagraph() 
$Date = Get-Date 
$myDoc.TypeText("Date: $($Date)") 
$doc.SaveAs([ref]$savepath) 
$doc.Close() 
$word.quit() 
Invoke-Item $savepath
#Here you want to release all of these objects, so here we call the ReleaseComObject method, so we doing this each of objects which created. 
#then you need to call garbage collection to scavenge memory, and I remove the variables.
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
Remove-Variable doc,Word
[gc]::collect()
[gc]::WaitForPendingFinalizers()

OUTPUT

 

Different Methods to Create a Directory with PowerShell

In Every day, sysadmins have to perform various standard operations on the numerous files and folders on their Windows servers like creating directory and copy files from one location to another, finding duplicated files etc., Here we will discuss how create a directory with the help of Powershell. For this, please open PowerShell console with administrative privileges.

Method 1: Using a file system object

$filesystemOBJ = new-object -ComObject scripting.filesystemobject
$filesystemOBJ.CreateFolder(“C:\dotnet-helpers\TestFolder1”)

Method 2: Using the new-item command

The New-Item cmdlet creates a new item and sets its value. The types of items that can be created depend on the location of the item.

new-item C:\dotnet-helpers\TestFolder2 -itemtype directory

Method 3: Using system.io.directory object

[system.io.directory]::CreateDirectory(“C:\dotnet-helpers\TestFolder3”)

Method 4: Use the md command

md C:\dotnet-helpers\TestFolder4

Output:

 

What do you think?

I hope you have an idea of  different methods for Creating a Directory with PowerShell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.

Creating a Balloon Tip Notification Using PowerShell

Why we need Balloon Tip Notification?

Mostly you will use common notification like display message in console using Write-* cmdlets to write the text to the console and Commonly we use Write-Error or Write-Warning for displaying Error and warnings.

Apart from displaying the message through the console, sometimes we required to get the user’s attention when something has completed or an issue is encountered. In that case, a pop-up window will helps you to built and displayed to the information/Warning/Error to the User. In this type of situation, we can implement the Balloon Tool Tip notification.

To achieve this, we need to work with the System.Windows.Forms.NotifyIcon type to build our notification balloon tip.

STEP 1# :

To get started, you need to load up an assembly to take advantage of a method that will help to extract the icon image from the file.

Add-Type -AssemblyName System.Windows.Forms

STEP 2# :

Second, you need to add the System.Windows.Forms assembly into our PowerShell session before we can make use of the NotifyIcon class. So your function would have to check whether there is already an icon that you can reuse.  This is done by using a “shared variable”, which really is a variable that has “script:” scope. Shared variables will be active their value as long as the script runs.

if ($script:balloonToolTip -eq $null)
{
$script:balloonToolTip = New-Object System.Windows.Forms.NotifyIcon
}

STEP 3# :

Then you want to set the system tray icon of the PowerShell ISE by just locating its path via Get-Process and locating the Path property. After you have the path, then you need to extract the icon from the file so it can be applied to the Icon property.Using this approach, you can pull any icon from a file and use it for the system tray icon when the balloon tip is launched.

As per above statement, the Get-Process -id $pid command gets the PowerShell process that is hosting the current session.The ExpandProperty expands the collections, that is, it will output the properties of every object in the collection. For example the out of $path variable will be “C:\windows\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe”

$path = Get-Process -id $pid | Select-Object -ExpandProperty Path

STEP 4# :

The ExtractAssociatedIcon which comes with the System.Drawing.Icon class and accepts a parameter of a string path name.

$balloonToolTip.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)

Full code

function ShowBalloonTipInfo 
{
[CmdletBinding()]
param
(
[Parameter()]
$Text,
[Parameter()]
$Title,
#It must be 'None','Info','Warning','Error'
$Icon = 'Info'
)
Add-Type -AssemblyName System.Windows.Forms
#So your function would have to check whether there is already an icon that you can reuse.This is done by using a "shared variable", which really is a variable that has "script:" scope.
if ($script:balloonToolTip -eq $null)
{
#we will need to add the System.Windows.Forms assembly into our PowerShell session before we can make use of the NotifyIcon class.
$script:balloonToolTip = New-Object System.Windows.Forms.NotifyIcon 
}
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path
$balloonToolTip.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloonToolTip.BalloonTipIcon = $Icon
$balloonToolTip.BalloonTipText = $Text
$balloonToolTip.BalloonTipTitle = $Title
$balloonToolTip.Visible = $true
#I thought to display the tool tip for one seconds,so i used 1000 milliseconds when I call ShowBalloonTip.
$balloonToolTip.ShowBalloonTip(1000)
}
ShowBalloonTipInfo ("The Notification from Dotnet-helpers : ","Read the latest topics of poweshell from dotnehelpers.com")

OUTPUT:

How To Display GUI POP UP message box in PowerShell

One of the simplest way to build simple message boxes in PowerShell is to “borrow” from .NET COM objects. Basically you initialize the object, and then you need to create instances of the object and finally call the Popup method. These messagebox code may be familiar to anyone who has programmed in vbScript, or any of the Microsoft programming languages such as Visual Basic or C#. The boxes can look pretty good, even though the coding is a bit monster.

In simple, all it really takes is two lines. One line to create the Wscript.Shell COM object and one to invoke the Popup method. Before the execution, let we discuss about the syntax below

Syntax :

intButton = objShell.Popup(strText,[nSecondsToWait],[strTitle],[nType])

Parameters :

strText String value containing the text you want to appear
in the pop-up message box.
nSecondsToWaitIt’s an optional. Maximum length of time to display the pop-up message
box (in seconds, Optional, default=infinite)
strTitleTitle text string, It’s an optional.
nTypeType of buttons and icons (Numeric, Optional)
These determine how the message box is used.

Button Types : OK = 1, Cancel = 2, Abort = 3, Retry = 4, Ignore = 5,                         Yes = 6, No = 7

Icon Types : 16  – “Stop Mark” icon. ,  32 – “Question Mark” icon, 48 – “Exclamation Mark” icon.  , 64 – “Information Mark” icon.

 

The command will write the return value of the clicked button to the pipeline as shown below

Button the return value :1 – OK button ,2 -Cancel button , 3 – Abort button , 4 – Retry button ,5  – Ignore button  , 6 – Yes button  , 7 –  No button

Example #1:

#creating object os WScript
$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
#invoking the POP method using object
$wshell.Popup("Are you want to continue from here?",5,"Hello User?",48+4)

Script Explanation:

From the syntax, please find parameter values.

strText : Are you want to continue from here?
nSecondsToWait : 5 (wait for 5 second)
strTitle : Hello User?
nType : 48+4 (Exclamation Mark icon + Yes and No buttons)
IntButton : Get the selected input as show in below table

OUTPUT:

What do you think?

I hope you have an idea of  How to display a pop-up message box with PowerShell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.