# Getting file metadata with PowerShell similar to what Windows Explorer provides

I’m working on a new feature for one of my modules that requires me to know what kind of files I am working with. It’s quite easy in PowerShell, and without a lot of code, you can reasonably quickly get necessary information about data stored on your desktop or anywhere else for that matter.

blog

20 Jun 2020 pl

![Getting file metadata with PowerShell similar to what Windows Explorer provides](/wp-content/uploads/2020/06/img_5eee2aa58d7b9.png)

[Zobacz obrazy (10)](#pf-content-image-1-0)

I'm working on a new feature for one of my modules that requires me to know **what kind of files I am working with**. It's quite easy in **PowerShell**, and without a lot of code, you can reasonably quickly get necessary information about data stored on your desktop or anywhere else for that matter.

```powershell
Get-ChildItem -Path $Env:USERPROFILE\Desktop -Force
```

[!\[Wynik polecenia PowerShell Get-ChildItem pokazujący pliki na pulpicie\](/wp-content/uploads/2020/06/img_5eee273c0f7cd.png)](/wp-content/uploads/2020/06/img_5eee273c0f7cd.png)

Or if you want to see all available properties for an object you can display it this way.

```powershell
Get-ChildItem -Path $Env:USERPROFILE\Desktop -Force | Select-Object -First 1 |  Format-List *
```

[!\[Wynik PowerShell Format-List pokazujący wszystkie właściwości pliku z pulpitu\](/wp-content/uploads/2020/06/img_5eee26fea7b0b.png)](/wp-content/uploads/2020/06/img_5eee26fea7b0b.png)

This, of course, is not all available data. If you right-click on any file and switch tab to **Details**, you will see a lot of special info is available. What's unique about it – is that each file type will have **different kinds of information**.

[!\[Karta Szczegóły w Eksploratorze Windows pokazująca metadane pliku Excel\](/wp-content/uploads/2020/06/img_5eee201485a32.png)](/wp-content/uploads/2020/06/img_5eee201485a32.png)[!\[Karta Szczegóły w Eksploratorze Windows pokazująca metadane instalatora MSI\](/wp-content/uploads/2020/06/img_5eee207e77a69.png)](/wp-content/uploads/2020/06/img_5eee207e77a69.png)[!\[Karta Szczegóły w Eksploratorze Windows pokazująca metadane pliku EXE\](/wp-content/uploads/2020/06/img_5eee20915fb8c.png)](/wp-content/uploads/2020/06/img_5eee20915fb8c.png)

As you can see above I've quickly checked **Excel file**, **MSI file**, an** EXE file**, all having similar but not the same information.

### 💡 Getting file metadata with PowerShell

Now that you know what I'm after, let me show you what I've done. With this small little function below, I can quickly get all the required information in no time. How do I use it? You can use it either via pipeline from **Get-ChildItem** or by providing full path inputs to it.

```powershell
# Option 1
Get-ChildItem -Path $Env:USERPROFILE\Desktop -Force | Get-FileMetaData -Signature | Out-HtmlView -ScrollX -Filtering -AllProperties

# Option 2
$Files = "$Env:USERPROFILE\Desktop\LAPS.x64.msi", "$Env:USERPROFILE\Desktop\DigiCertUtil.exe"
$Files | Get-FileMetaData -Signature | Out-HtmlView -ScrollX -Filtering -AllProperties

# Option 3
Get-FileMetaData -File $Files | Out-HtmlView -ScrollX -Filtering -AllProperties

# Option 4
Get-ChildItem -Path $Env:USERPROFILE\Desktop -Force | Where-Object { $_.Attributes -like '*Hidden*' } | Get-FileMetaData -Signature | Out-HtmlView -ScrollX -Filtering -AllProperties
```

This function has two parameters (for now) – one taking **[System.IO.DirectoryInfo]** (basically Get-ChildItem output) or **[string]**, the other one is a switch deciding whether you want to check if the file is signed or not.

[!\[Raport Out-HtmlView pokazujący metadane plików w przeszukiwalnej tabeli\](/wp-content/uploads/2020/06/img_5eee26957a5f2.png)](/wp-content/uploads/2020/06/img_5eee26957a5f2.png)[!\[Raport Out-HtmlView pokazujący rozszerzone metadane i informacje o podpisie plików\](/wp-content/uploads/2020/06/img_5eee26b367b79.png)](/wp-content/uploads/2020/06/img_5eee26b367b79.png)[!\[Raport Out-HtmlView porównujący metadane wielu typów plików\](/wp-content/uploads/2020/06/img_5eee26cfbc90b.png)](/wp-content/uploads/2020/06/img_5eee26cfbc90b.png)

The output you see above is only output from two files. I'm specifically using the [PSWriteHTML Out-HtmlView](https://github.com/EvotecIT/PSWriteHTML) command with the **ScrollX** parameter to make sure it shows everything that there is to show. Additionally, please notice I'm using **AllProperties** switch. This switch is special when it comes to **Out-HtmlView**. This is because most of the commands such as **Format-Table** or **Export-CSV** or even **Out-HtmlView**, by default, display properties only from 1st entry in the array. It's efficient and fast. However, in the case of **Get-FileMetaData,** where each file has different properties it doesn't make much sense. Using **AllProperties** does a “prescan” of an Array, and then any property missing from 1st element is added so that **Out-HtmlView** can display everything there is to show. Of course, if you're working with the same file types that don't differ from each other, you can skip that parameter or **Out-HtmlView** for that matter.

[!\[Tabela Out-HtmlView pokazująca wszystkie kolumny metadanych z różnych typów plików\](/wp-content/uploads/2020/06/img_5eee2e8d954e9.png)](/wp-content/uploads/2020/06/img_5eee2e8d954e9.png)

If you just want to peek into an object quickly, you can use **Format-List *** to see all parameters without using **Out-HtmlView**.

```powershell
$Files = "$Env:USERPROFILE\Desktop\LAPS.x64.msi", "$Env:USERPROFILE\Desktop\DigiCertUtil.exe"
$Files | Get-FileMetaData -Signature | Format-List
```

[!\[Wynik PowerShell Format-List dla danych zwracanych przez Get-FileMetaData\](/wp-content/uploads/2020/06/img_5eee2aa58d7b9.png)](/wp-content/uploads/2020/06/img_5eee2aa58d7b9.png)

If you intend to use **Out-HtmlView** for this, please make sure to install it first. You can do so using **Install-Module**

```powershell
Install-Module PSWriteHTML -Force
```

Ok, so now that you know what you're getting into, there are two ways to get **Get-FileMetaData** function. Copy and paste from the source below or install one of my “do it all” modules called **PSSharedGoods**. I use the **PSSharedGoods** module as my glue module where multiple other modules use it as a dependency, or I copy the functions from it during **build tim**e and convert them to private services within other modules to minimize dependencies.

```powershell
Install-Module PSSharedGoods -Force
```

Keep in mind that I update my modules quite often, fixing bugs, so always up to date versions of said modules can be downloaded from **PowerShellGallery** using **Install-Module,** as mentioned above. When I push my modules into **PowerShellGallery,** I minimize them and optimize them for production use. If you're into development and want to work in a more structured way where each function has separate files, multiple folders, and so on, I would highly recommend visiting my **GitHub** pages for [PSWriteHTML](https://github.com/EvotecIT/PSWriteHTML) or [PSSharedGoods](https://github.com/EvotecIT/PSSharedGoods).

### 💡 Getting file metadata with PowerShell

Here's the function in all it's glory for you to copy/paste and use as you see fit.

```powershell
function Get-FileMetaData {

    [CmdletBinding()]
    param (
        [Parameter(Position = 0, ValueFromPipeline)][Object] $File,
        [switch] $Signature
    )
    Process {
        foreach ($F in $File) {
            $MetaDataObject = [ordered] @{}
            if ($F -is [string]) {
                $FileInformation = Get-ItemProperty -Path $F
            } elseif ($F -is [System.IO.DirectoryInfo]) {
                #Write-Warning "Get-FileMetaData - Directories are not supported. Skipping $F."
                continue
            } elseif ($F -is [System.IO.FileInfo]) {
                $FileInformation = $F
            } else {
                Write-Warning "Get-FileMetaData - Only files are supported. Skipping $F."
                continue
            }
            $ShellApplication = New-Object -ComObject Shell.Application
            $ShellFolder = $ShellApplication.Namespace($FileInformation.Directory.FullName)
            $ShellFile = $ShellFolder.ParseName($FileInformation.Name)
            $MetaDataProperties = [ordered] @{}
            0..400 | ForEach-Object -Process {
                $DataValue = $ShellFolder.GetDetailsOf($null, $_)
                $PropertyValue = (Get-Culture).TextInfo.ToTitleCase($DataValue.Trim()).Replace(' ', '')
                if ($PropertyValue -ne '') {
                    $MetaDataProperties["$_"] = $PropertyValue
                }
            }
            foreach ($Key in $MetaDataProperties.Keys) {
                $Property = $MetaDataProperties[$Key]
                $Value = $ShellFolder.GetDetailsOf($ShellFile, [int] $Key)
                if ($Property -in 'Attributes', 'Folder', 'Type', 'SpaceFree', 'TotalSize', 'SpaceUsed') {
                    continue
                }
                If (($null -ne $Value) -and ($Value -ne '')) {
                    $MetaDataObject["$Property"] = $Value
                }
            }
            if ($FileInformation.VersionInfo) {
                $SplitInfo = ([string] $FileInformation.VersionInfo).Split([char]13)
                foreach ($Item in $SplitInfo) {
                    $Property = $Item.Split(":").Trim()
                    if ($Property[0] -and $Property[1] -ne '') {
                        $MetaDataObject["$($Property[0])"] = $Property[1]
                    }
                }
            }
            $MetaDataObject["Attributes"] = $FileInformation.Attributes
            $MetaDataObject['IsReadOnly'] = $FileInformation.IsReadOnly
            $MetaDataObject['IsHidden'] = $FileInformation.Attributes -like '*Hidden*'
            $MetaDataObject['IsSystem'] = $FileInformation.Attributes -like '*System*'
            if ($Signature) {
                $DigitalSignature = Get-AuthenticodeSignature -FilePath $FileInformation.Fullname
                $MetaDataObject['SignatureCertificateSubject'] = $DigitalSignature.SignerCertificate.Subject
                $MetaDataObject['SignatureCertificateIssuer'] = $DigitalSignature.SignerCertificate.Issuer
                $MetaDataObject['SignatureCertificateSerialNumber'] = $DigitalSignature.SignerCertificate.SerialNumber
                $MetaDataObject['SignatureCertificateNotBefore'] = $DigitalSignature.SignerCertificate.NotBefore
                $MetaDataObject['SignatureCertificateNotAfter'] = $DigitalSignature.SignerCertificate.NotAfter
                $MetaDataObject['SignatureCertificateThumbprint'] = $DigitalSignature.SignerCertificate.Thumbprint
                $MetaDataObject['SignatureStatus'] = $DigitalSignature.Status
                $MetaDataObject['IsOSBinary'] = $DigitalSignature.IsOSBinary
            }
            [PSCustomObject] $MetaDataObject
        }
    }
}
```

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:98D537B78472CECCAAEA0D775965D3C8B369CC520E44B4B371210C6827A0476E -->
