Advertisement
Wasif_Hasan_

Get-HashedProperty (Powershell Function)

Aug 14th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Get-HashedProperty
  2. # This is actually a function
  3. # It will save the properties and values of a registry key in key-value pair and return in Hashtable/JSON
  4. # It will also exclude PS* Properties
  5. # You can add this into your $PROFILE
  6. # Usage: Get-HashedProperty "Registry Path" -As "JSON/Hashtable"
  7. # -As switch is optional, default value is Hashtable
  8.  
  9. Function Get-HashedProperty {
  10.   [CmdletBinding()]
  11.   Param(    
  12.     [Parameter(Mandatory=$True)]
  13.     [ValidateScript({Test-Path $_})]
  14.     [String]$RegPath,
  15.     [Parameter(Mandatory=$False)]
  16.     [ValidateSet("Json","HashTable")]
  17.     [String]$As
  18.   )
  19.   $Hash = @{}
  20.   Get-ItemProperty "$RegPath" |
  21.     Get-Member -MemberType NoteProperty |
  22.       Where-Object {$_.Name -notlike "PS*"} | Foreach {
  23.         $_ | Select-Object -ExpandProperty Name | Foreach {
  24.           $Value = Get-ItemProperty "$RegPath" -Name "$_"
  25.           $Hash.Add("$_","$($Value."$_")")
  26.         }
  27.       }
  28.    If($As -eq "Json"){
  29.      $Hash = $Hash | ConvertTo-Json
  30.    }
  31.    Return $Hash
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement