StPeteAPB
StPeteAPB

Reputation: 43

Comparable |sh for Powershell?

I have a system where a ton of folks are logging in and getting temporary profiles. So, I want to query the Registry for ".bak" profiles sand then query those results for the C:\Users folders so I can then determine if their folder is there or not.

So far, I have this:

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | findstr /i ".bak" | %{$_ -replace "^","reg query """} |%{$_ -replace "$",""" | findstr ProfileImagePath >> d:\temp\Results.dat"}

That results in commands like these:

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-9876543238-1234562203-7654325021-1001" | findstr ProfileImagePath >> d:\temp\Results.dat
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-1234563238-1234562203-7654325021-1003" | findstr ProfileImagePath >> d:\temp\Results.dat

In the Unix/Linux world, using SED, I could then pipe this to run ( | sh ). Is there a comparable Powershell call where I can pipe the reformatted command to now run?

Upvotes: 0

Views: 117

Answers (1)

js2010
js2010

Reputation: 27516

I would do something something like this. Pass the key name over the pipe to get-itemproperty, then pick the profileimagepath property with select-object.

get-item 'HKLM:\software\Microsoft\Windows NT\CurrentVersion\ProfileList\*.bak' | 
  get-itemproperty -Name ProfileImagePath | 
  select-object ProfileImagePath


ProfileImagePath
----------------
c:\users\fake
c:\users\fake2

Here's a way to use test-path with it:

get-item 'HKLM:\software\Microsoft\Windows NT\CurrentVersion\ProfileList\*.bak' |
  get-itemproperty |
  select-object ProfileImagePath, @{name='Exists'; 
  expression={test-path $_.ProfileImagePath}}


ProfileImagePath Exists
---------------- ------
c:\users\fake      True
c:\users\fake2    False

Upvotes: 1

Related Questions