-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathGet Extended File Properties.ps1
107 lines (83 loc) · 2.91 KB
/
Get Extended File Properties.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#requires -version 3
<#
Get the named extended property(s) from the file or all available properties
With code from https://rkeithhill.wordpress.com/2005/12/10/msh-get-extended-properties-of-a-file/
@guyrleech 17/12/2019
#>
<#
.SYNOPSIS
Get the named extended property(s) from the file or all available properties
.DESCRIPTION
Useful where a file has extended properties/metadata such as an image file or msi file
.PARAMETER filename
The name of the file to retrieve the properties of
.PARAMETER properties
Comma separated list of the properties to retrieve. If not specified, will return all available properties
.EXAMPLE
Get-ExtendedProperties -fileName "C:\@guyrleech\googlechromestandaloneenterprise64.v79.msi" -properties Title,Subject,Categories,Tags,Comments,Name,Authors
Returns the requested properties for the specified file
.EXAMPLE
Get-ExtendedProperties -fileName "C:\@guyrleech\picture.jpg"
Returns all extended properties for the specified file
#>
Function Get-ExtendedProperties
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,HelpMessage='File name to retrieve properties of')]
[ValidateScript({Test-Path -Path $_})]
[string]$fileName ,
[AllowNull()]
[string[]]$properties
)
[hashtable]$propertiesToIndex = @{}
## need to use absolute paths
$fileName = Resolve-Path -Path $fileName | Select-Object -ExpandProperty Path
$shellApp = New-Object -Com shell.application
$myFolder = $shellApp.Namespace( (Split-Path -Path $fileName -Parent) )
$myFile = $myFolder.Items().Item( (Split-Path -Path $fileName -Leaf) )
0..500 | ForEach-Object `
{
If( $key = $myFolder.GetDetailsOf( $null , $_ ) )
{
Try
{
$propertiesToIndex.Add( $key , $_ )
}
Catch
{
}
}
}
Write-Verbose "Got $($propertiesToIndex.Count) unique property names"
If( ! $PSBoundParameters[ 'properties' ] -or ! $properties -or ! $properties.Count )
{
ForEach( $property in $propertiesToIndex.GetEnumerator() )
{
$thisProperty = $myFolder.GetDetailsOf( $myFile , $property.Value )
If( ! [string]::IsNullOrEmpty( $thisProperty ) )
{
[pscustomobject]@{
'Property' = $property.Name
'Value' = $thisProperty
}
}
}
}
Else
{
ForEach( $property in $properties )
{
$index = $propertiesToIndex[ $property ]
If( $index -ne $null )
{
$myFolder.GetDetailsOf( $myFile , $index -as [int] )
}
Else
{
Write-Warning "No index for property `"$property`""
}
}
}
}