TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
PowerShell Search XML by selected option
Dorababu Meka
Jul 20
2016
Code
763
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
Sample XML that I used
<
Employees
>
<
Employee
>
<
Name
>
TestName
</
Name
>
<
Age
>
32
</
Age
>
<
Location
>
Hyderabad
</
Location
>
</
Employee
>
<
Employee
>
<
Name
>
TestName1
</
Name
>
<
Age
>
29
</
Age
>
<
Location
>
Hyderabad
</
Location
>
</
Employee
>
<
Employee
>
<
Name
>
TestName3
</
Name
>
<
Age
>
45
</
Age
>
<
Location
>
Rajahmundry
</
Location
>
</
Employee
>
</
Employees
>
PowerShell script
?cls
function XmlOperations
{
$xml = [xml](Get-Content C:\Dorababu\sample.xml)
Write-Host
"Please choose an option `n 1) Based on Name `n 2) Based on Age `n 3) Based on Location `n"
$inputNumber = Read-Host
#$inputNumber = Read-Host "Please select an option `n`n 1) Based on Name `n 2) Based on Age `n 3) Based on Location `n " -NoNewline
$SearchColumn = @()
switch
($inputNumber)
{
1{ $SearchColumn = Read-Host -Prompt
"`n Enter Name"
$xml.Employees.Employee | where { $_.Name -like
"*$SearchColumn*"
} }
2{ $SearchColumn = Read-Host -Prompt
"`n Enter Age"
$xml.Employees.Employee | where { $_.Age -like
"*$SearchColumn*"
} }
3{ $SearchColumn = Read-Host -Prompt
"`n Enter Location"
$xml.Employees.Employee | where { $_.Location -like
"*$SearchColumn*"
} }
default
{
$SearchColumn = Read-Host -Prompt
"`n Enter Name"
$xml.Employees.Employee | where { $_.Name -like
"*$SearchColumn*"
} }
}
}
XmlOperations
PowerShell