Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## PowerShell to turn cmdkey.exe output into objects
- ## Could be used for parsing other text - uses blank line as terminator for an object but check for first field name if no blank line
- ## 1 liner for copy/paste into PS command line
- $o = $null ; cmdkey /list | Select -skip 2 | % { $f = $_.Trim() ; if( $f -match '^\s*$' ) { if( $o ) { $o ; $o = $null } } else { if( -Not $o ) { $o = New-Object pscustomobject }; if( $f.IndexOf( ':' ) -gt 0 ) { $s = ($f -split ':' , 2).Trim() ; Add-Member -InputObject $o -MemberType NoteProperty -Name $s[0] -Value $s[-1] } else { Add-Member -InputObject $o -MemberType NoteProperty -Name 'Persistence' -Value $f } } }
- ## For use in scripts with vaguely meaningful variable names and formatted better
- $object = $null
- cmdkey /list | Select-Object -skip 2 | ForEach-Object {
- $field = $_.Trim()
- if( $field -match '^\s*$' ) {
- if( $object ) {
- $object
- $object = $null }
- } else {
- if( -Not $object ) {
- $object = New-Object pscustomobject }
- ## one field is not : delimited so have to treat as special
- if( $field.IndexOf( ':' ) -gt 0 ) {
- $splits = ($field -split ':' , 2).Trim()
- Add-Member -InputObject $object -MemberType NoteProperty -Name $splits[0] -Value $splits[-1]
- } else {
- Add-Member -InputObject $object -MemberType NoteProperty -Name 'Persistence' -Value $field
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement