-
Notifications
You must be signed in to change notification settings - Fork 3
/
Export-Registry.ps1
182 lines (156 loc) · 6.09 KB
/
Export-Registry.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function Export-Registry
{
<#
.SYNOPSIS
Export registry item properties.
.DESCRIPTION
Export item properties for a given registry key.
By default results will be written to the pipeline unless the -ExportFormat parameter is used.
.PARAMETER KeyPath
A string representing the Key(s) to export in the PsDrive format IE: HKCU:\SOFTWARE\TestSoftware
.PARAMETER ExportFormat
A string representing the format to use for the export.
Possible values are:
- CSV
- XML
PArameter is used in conjunction with the ExportPath paramter.
.PARAMETER ExportPath
A string representing the path where keys should be exported.
.PARAMETER NoBinaryData
When parameter is specified any binary data present in the registry key is removed.
.EXAMPLE
PS C:\> Export-RegistryNew -KeyPath 'HKCU:\SOFTWARE\TestSoftware'
.NOTES
Additional information about the function.
#>
[CmdletBinding(DefaultParameterSetName = 'PrintOnly')]
param
(
[Parameter(ParameterSetName = 'PrintOnly',
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0,
HelpMessage = 'Enter a registry path using the PSDrive format (IE: HKCU:\SOFTWARE\TestSoftware')]
[Parameter(ParameterSetName = 'Export',
Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Alias('PSPath')]
[string[]]
$KeyPath,
[Parameter(ParameterSetName = 'Export',
Mandatory = $true)]
[ValidateSet('xml', 'csv', 'reg', IgnoreCase = $true)]
[ValidateNotNullOrEmpty()]
[string]
$ExportFormat,
[Parameter(ParameterSetName = 'Export',
Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$ExportPath,
[Parameter(ParameterSetName = 'Export')]
[switch]
$NoBinaryData
)
begin
{
# Initialize results array
[System.Collections.ArrayList]$returnData = @()
}
process
{
# Go through all paths
foreach ($path in $KeyPath)
{
if ((Test-IsRegistryKey -KeyPath $path) -eq $true)
{
Write-Verbose "Getting properties for key: $path"
# Get registry item
$paramGetItem = @{
Path = $path
ErrorAction = 'Stop'
}
[Microsoft.Win32.RegistryKey]$regItem = Get-Item @paramGetItem
# Get key properties
[array]$regItemProperties = $regItem.'Property'
if ($regItemProperties.Count -gt 0)
{
# Enumerate properties
foreach ($property in $regItemProperties)
{
Write-Verbose "Exporting $property"
# Append data to return array
[void]($returnData.Add([pscustomobject]@{
'Path' = $regItem
'Name' = $property
'Value' = $regItem.GetValue($property, $null, 'DoNotExpandEnvironmentNames')
'Type' = $regItem.GetValueKind($property)
'Computername' = $env:computername
}))
}
}
else
{
# Return default object
[void]($returnData.Add([pscustomobject]@{
'Path' = $regItem
'Name' = '(Default)'
'Value' = $null
'Type' = 'String'
'Computername' = $env:computername
}))
}
}
else
{
Write-Warning -Message "Key $path does not exist"
continue
}
}
}
end
{
# Check we have results
if ($null -ne $returnData)
{
switch ($PSCmdlet.ParameterSetName)
{
'Export'
{
# Remove binary data
if ($PSBoundParameters.ContainsKey('NoBinaryData'))
{
Write-Verbose -Message 'Removing binary data from return values'
# Remove binary data
$returnData = $returnData | Where-Object { $_.Type -ne 'Binary' }
}
switch ($ExportFormat)
{
'csv'
{
# Export to CSV
$returnData | Export-Csv -Path $ExportPath -NoTypeInformation -Force
}
'xml'
{
# Export to XML and overwrite
$returnData | Export-Clixml -Path $ExportPath -Force
}
}
Write-Verbose -Message "Data written to $ExportPath"
}
default
{
Write-Verbose -Message 'No data will be exported'
# Print on screen only
$returnData
}
}
}
else
{
Write-Warning -Message 'No found - No export will be created'
}
}
}