Reading XML with the XmlReader in Powershell

The XmlReader class in .Net Framework reads XML data from a stream or file. It provides non-cached, forward-only, read-only access to XML data. I wrote a simple program in powershell to explore XmlReader class as belows. Have fun!

A sample XML input file: books.xml

<?xml version="1.0"?>
<books>
 <book> 
   <author>Carson</author>
   <price>31.95</price>
   <pubdate>05/01/2001</pubdate>
 </book>
 <pubinfo>
   <publisher>MSPress</publisher>
   <state>WA</state>
 </pubinfo>
</books>

A powershell script: xmlreader1.ps1

##  
# xmlreader1.ps1  
## 
$PWD=Get-Location
$xmlfile=$PWD.Path+"\books.xml"

# Create XML Reader  
$reader = [system.Xml.XmlReader]::Create($xmlfile) 

# Parse the XML document.  
$result=$reader.Read()  
$reader.ReadStartElement("books")

# start book node
"The following is from book:"
$reader.ReadStartElement("book")

# Read and Display auther  
$reader.ReadStartElement("author")     
"author: {0}" -f $reader.ReadString()  
$reader.ReadEndElement()  

# Read and display price  
$reader.ReadStartElement("price")  
"price: {0}" -f $reader.ReadString()  
$reader.ReadEndElement() 
# Read and display pubdate
$reader.ReadStartElement("pubdate")  
"pubdate: {0}" -f $reader.ReadString()  
$reader.ReadEndElement()

# end book node
""
$reader.ReadEndElement()

# start pubinfo node
"The following is from pubinfo:"
$reader.ReadStartElement("pubinfo")

# Read and Display publisher  
$reader.ReadStartElement("publisher")     
"publisher: {0}" -f $reader.ReadString()  
$reader.ReadEndElement()  

# Read and display state  
$reader.ReadStartElement("state")  
"state: {0}" -f 
$reader.ReadString()  

$reader.ReadEndElement() 
# end pubinfo node

$reader.ReadEndElement()
# End of Script 

Execution Result

PS C:\henry416\> .\xmlreader1.ps1

The following is from book:
author: Carson
price: 31.95
pubdate: 05/01/2001

The following is from pubinfo:
publisher: MSPress
state: WA

Delete older files using Powershell?

One of thing I like linux (unix) is the scripting. The one I like most is to remove files older than 10 days:

find /yourpath/yourfiles* -type f -mtime +10 -exec rm '{}' \;

But I  need to do this in Windows Powershell, and how? Here is the answer:

$Now=get-date

$LastWrite = $Now.AddDays(-10)

Get-ChildItem \yourpath\yourfile* |Where {$_.LastWriteTime -le "$LastWrite"}|remove-item -recurse

Resource Usage in Powershell Script

To get all the detail from processors

Get-WmiObject win32_processor

To shows the CPU load in every 5 sec

while ($true) {
    Start-Sleep -s 5
    cls
    ‘CPU Load is’
    Get-WmiObject win32_processor | select LoadPercentage  |fl
}

To list all the processes (like ps in Linux)

get-process
will produce the following preocess list...
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    146       7     4992       1844    52     0.92    268 ACLIENT
     59       8     1188       2176    31     0.11   5948 AClntUsr
    136       5     4496       7132    49     4.55   5412 acrotray
    139       3     1728       3540    33     0.30   4192 AeXAgentUIHost
   1792     143    54748      11296   270   570.22    924 AeXNSAgent
    110       5     1364       1784    33            2388 alg
    257       6    15080      13104    89   124.14   1744 ArelliaACSvc
     31       2      488        764    15     0.06    956 CCSRVC
    113       4     1636       2928    42     0.08   2276 Client
     33       2     2160       2996    30     0.02   3588 cmd
     61       3     1384       1432    25     0.05   1952 CNTAoSMgr
    949      23    35120      31764   200    11.52   4216 communicator
    123       4     1532       2540    41     0.03   5132 concentr
   1102       8     3712       9140    74            2036 csrss
     79       4     1108       2364    31     0.06   5208 ctfmon
    675    5719    27204      26284   137   245.34   3212 explorer
     47       2     1572       2368    19     2.42   4244 FNPLicensingService
     91       3     1176       1840    33     0.06   3512 hkcmd
     29       2      576       1204    16     0.03   2332 hpmup094.bin
     90       5     4476       3504    52     0.03   6080 hqtray

get-process | where-object {$_.WorkingSet -gt 10000000}
to get those process name with higher working set.
stop-process –name processname
can be used to stop (kill) those processes

Some references

PowershellPro

The Powershell Guy

The Po$herLife

 
Design a site like this with WordPress.com
Get started