PowerShell – File Hash

Many online file downloads have a checksum in the form of a ‘hash’ code, allowing you to verify the file integrity. This can be useful when downloaded via a 3rd party mirror or to see if a file is corrupted.

There is a handy PowerShell command for this: Get-FileHash

The -Algorithm parameter allows you to get the hash for different algorithms. Use <tab> to cycle through the available options.

Taking a LibreOffice mirror download as an example.

  • Filename: LibreOffice_6.4.5_Win_x64.msi
  • SHA-256 Hash3d80c1d8f3bf4540a6b845d43e54d3e6940058a9b2e5456234ac0fa39527ebcf
  • SHA-1 Hash727e65bfbd50d8a6953687398c62ad8e688256e1
  • MD5 Hashfa9c89d871b1e8d520ae2d488bdbf87d

 

 

 

PowerShell – XML

This is how easy it is to parse an XML file with PowerShell!

[xml]$xml = Get-Content .\data.xml

Example XML:

<?xml version="1.0" encoding="UTF-8"?>
<Entries>
    <Entry Category="Animal">Dog</Entry>
    <Entry Category="Animal">Cat</Entry>
    <Entry Category="Animal">Cow</Entry>
    <Entry Category="City">New York</Entry>
    <Entry Category="City">Paris</Entry>
    <Entry Category="City">London</Entry>
</Entries>

Example using the XML data:

foreach($em in $xml.Entries.Entry) { Write-Host "$($em.InnerText) is a type of $($em.Attributes['Category'].Value)" }
Dog is a type of Animal
Cat is a type of Animal
Cow is a type of Animal
New York is a type of City
Paris is a type of City
London is a type of City

XPath support is also built-in:

$xml.Entries.SelectNodes("//Entry[contains(@Category, 'City')]")
Category #text
-------- -----
City New York
City Paris
City London

To get more information on the return type of a statement use the GetType() method.