Using the below code, I get Name & LastLogon populated, but not ProfilePath.
Add-RegKeyMember is https://gallery.technet.microsoft.com/scriptcenter/Get-Last-Write-Time-and-06dcf3fb .
I have tried to access ProfileImagePath with $Profile.Properties.ProfileImagePath, $Profile.Name.ProfileImagePath, and others, but they all return blank (could be null). How on earth is this seemingly object making these properties available?
$Profiles = get-childitem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Add-RegKeyMember
foreach($Profile in $Profiles)
{
$ThisProfileInfo = #{Name=$Profile.Name;
LastLogon=$Profile.LastWriteTime;
ProfilePath=$Profile.ProfileImagePath}
$Profile
}
Name Property
---- --------
S-1-5-18 Flags : 12
ProfileImagePath : C:\WINDOWS\system32\config\systemprofile
RefCount : 1
Sid : {1, 1, 0, 0...}
State : 0
This is because [Microsoft.Win32.RegistryKey] object returns properties as string array. You should simply retrieve the value ProfileImagePath from the object itself :
ProfilePath=$Profile.GetValue("ProfileImagePath")
Please see the below adjustments to your script.
You can pull the sub values of property by using the method GetValue.
I have also adjusted how you are storing each iteration and outputting the value post the foreach loop as the example above will just output each $profile as it was before the loop.
I have not tested with Add-RegKeyMember and therefore I am unable to confirm if this will pull the LastWriteTime property, but I can confirm that this will pull the profileimagepath property.
$Profiles = get-childitem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Add-RegKeyMember
$ProfileData = #()
foreach($Profile in $Profiles){
$ThisProfileInfo = $null
$ThisProfileInfo = #{Name=$Profile.Name;
LastLogon=$Profile.GetValue("LastWriteTime");
ProfilePath=$Profile.GetValue("ProfileImagePath")}
$ProfileData += $ThisProfileInfo
}
$ProfileData
Related
How can I past only a selection of fields from an object to show in out-gridview, but still keep the whole object as a result.
For example, I retrieve an object that gives me:
$listcreds = get-listofcredentials
Id : 03c0e0c0-0951-4698-9ba9-a70508b5467f
IsLocalProtect : True
Name : vsphere.local\Administrator
CurrentUser : False
UserName : vsphere.local\Administrator
UserNameAtNotation : Administrator#vsphere.local
UserNameSlashNotation : vsphere.local\Administrator
UserNameOnly : Administrator
DomainName : vsphere.local
EncryptedPassword : Veeam.Backup.Common.CCodedPassword
In the gridview I want to only see Name and ID. After the user selects the row desired, I would like to have the result as the same type of object again.
When I use select-object,
$selectedcred = $listofcredentials | select-object Name, Id | out-gridview -passthru
I get the result back but I would now have to search the original object again to get the other properties of that row.
What better way to do this?
You need to find the full object again in the list with Where-Object
This answer assumes the Id property is unique for every item in $listcreds
$listcreds = get-listofcredentials
$selectedcred = $listofcredentials | select-object Name, Id | out-gridview -passthru
$selectedcred = $listcreds | Where-Object {$_.Id -eq $selectedcred.Id}
I don't think there's a better solution here. If performance is a concern, you can convert the Where-Object into a foreach as below:
$listcreds = get-listofcredentials
$selectedcred = $listofcredentials | select-object Name, Id | out-gridview -passthru
foreach ($cred in $listcreds) {
if ($cred.Id -eq $selectedcred.Id) {
$selectedcred = $cred
break
}
}
However, the performance difference may be negligible or even negative.
I am trying to write a PowerShell script that iterates through the objects properties and outputs only the ones that have a value of True.
My starting data is this:
UserId : 00546000000m3vCAAQ
UserPermissionsOfflineUser : True
UserPermissionsMobileUser : False
UserPermissionsSupportUser : True
UserPermissionsWebUser : False
I would like the output to consist of the UserID and only the licenses that are true like the following:
UserId : 00546000000m3vCAAQ
UserPermissionsOfflineUser : True
UserPermissionsSupportUser : True
I think I need two loops, one to iterate over each user and then another loop to iterate over the user's properties and just parse out all the false values.
foreach ($_ in $resultset)
{
$_ |
Select-Object -Property #{ N = 'UserId'; E = { $_.Id } }
#This is where I am getting stuck on the second loop.
#$_.psobject.properties | % { $_.Value }
}
Use Where-Object to find the properties with a value of $true, then use Select-Object to select those along with the UserID property:
$resultSet |ForEach-Object {
$trueProperties = $_.psobject.Properties |Where-Object {$_.Value -eq $true} |Select -ExpandProperty Name
$_ |Select -Property #('Id';$trueProperties)
}
Be aware that selectively picking out properties based on their value like this may cause you pain later on with Export-* or Format-* cmdlets
The |Select-Object -ExpandProperty Name part grabs the values of the Name property of the properties that made it through our Where-Object filter, so at this point $trueProperties contain 2 strings: "UserPermissionsOfflineUser" and "UserPermissionsSupportUser".
The expression #('Id';$trueProperties) simply concatenates the strings to the "Id" string, so the last Select-Object statement is basically the same as saying:
$_ |Select Id,UserPermissionsOfflineUser,UserPermissionsSupportUser
I am creating my first application with VS and PowerShell and need to get the name of a service from my listview. The output of the selection looks like this:
ComputerName Status Name DisplayName
------------ ------ ---- -----------
PC Running Appinfo Application Information
What I want to do is get the value Appinfo from the Name column and assign it to a variable. I've had no luck with regex, but then again I am a beginner so I could be doing something wrong. Is there an easy way to do this?
The output you're currently getting is a formatted (tabular) view on selected properties of an object (namely the properties ComputerName, Status, Name, and DisplayName). You can get the value of a particular property by expanding it via the Select-Object cmdlet:
$name = ... | Select-Object -Expand Name
You could also store the object in a variable and access the property via dot-notation:
$obj = ...
$name = $obj.Name
Beware that if a cmdlet outputs multiple objects the variable will contain an array:
PS C:\> $services = Get-Service
PS C:\> $services.GetType().FullName
System.Object[]
PS C:\> $services.Count
136
You can access properties of a member object by index:
$services[42].Name
or by selecting a specific object via its properties:
$services | Where-Object { $_.DisplayName -eq 'Application Information' } |
Select-Object -Expand Name
Since PowerShell v3 you can also use the dot-property notation to get the value of a particular property of all array members:
$services.Name
Prior to PowerShell v3 this would have thrown an error, because the array object doesn't have a property Name.
Suppose I call Get-Service and want to assign a new column ID with the cmdlet output that prints incrementing integers so that:
ID Status Name DisplayName
-- ------ ---- -----------
0 Running AdobeARMservice Adobe Acrobat Update Service
1 Stopped AeLookupSvc Application Experience
2 Stopped ALG Application Layer Gateway Service
I'm trying to use Select-Object right now to add this column, but I don't quite understand how to iterate a variable in this sort of expression. Here's what I've got:
Get-Service |
Select-Object #{ Name = "ID" ; Expression= { } }, Status, Name, DisplayName |
Format-Table -Autosize
Is there a way to iterate integers within Expression= { }, or am I going about this problem the wrong way?
You can do it this way, though you will need to maintain some counter variable outside of the main expression.
$counter = 0
Get-Service |
Select-Object #{ Name = "ID" ; Expression= {$global:counter; $global:counter++} }, Status, Name, DisplayName |
Format-Table -Autosize
Another option, which is perhaps cleaner
Get-Service `
|% {$counter = -1} {$counter++; $_ | Add-Member -Name ID -Value $counter -MemberType NoteProperty -PassThru} `
| Format-Table ID
I asked the same question a different way and got the following answer
$x = 10
Get-Service |
Select-Object #{ Name = "ID" ; Expression={ (([ref]$x).Value++) }}, Status, Name, DisplayName | Format-Table -Autosize
It wasn't at all clear to me that the expression is being invoked within Select-Object's scope, not the pipe's. The [ref] qualifier bumps the increment's result up to the pipe's scope achieving the same result as explicitly specifying the variable as global.
I'm trying to get a feel for the idioms to use in PowerShell.
Given this script:
$path = 'hkcu:\Software\Microsoft\Windows\CurrentVersion\Extensions'
$key = Get-Item $path
$key
I get the output at the bottom of this question.
I'd like to get output of the properties (the name/value pairs under the $key) where I can filter on both name and value.
For instance, filter to list all the Extensions that have:
name like xls*
or value like *\MSACCESS.EXE
Or an exclude filter: exclude all names like doc*
Fir the first filter, I'd want a result like this:
Name Value
---- --------
xlsx C:\PROGRA~2\MICROS~1\Office15\EXCEL.EXE
xls C:\PROGRA~2\MICROS~1\Office15\EXCEL.EXE
mdb C:\PROGRA~2\MICROS~1\Office15\MSACCESS.EXE
mda C:\PROGRA~2\MICROS~1\Office15\MSACCESS.EXE
This is the original output of the script:
Hive: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion
Name Property
---- --------
Extensions rtf : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.rtf
dot : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.dot
dotm : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.dotm
dotx : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.dotx
docm : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.docm
docx : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.docx
doc : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.doc
xlsx : C:\PROGRA~2\MICROS~1\Office15\EXCEL.EXE
xls : C:\PROGRA~2\MICROS~1\Office15\EXCEL.EXE
mdb : C:\PROGRA~2\MICROS~1\Office15\MSACCESS.EXE
mda : C:\PROGRA~2\MICROS~1\Office15\MSACCESS.EXE
Edit
I solved part of the problem: getting a list of Name/Value pairs. It uses PSCustomObject:
$namevalues = $key.GetValueNames() | ForEach-Object { [pscustomobject]#{ Name=$_; Value=$key.GetValue($_) } }
$namevalues
(How should I wrap that code?)
Any help with the filtering would be much appreciated
A two part answer.
We start with the $key from the registry:
$path = 'hkcu:\Software\Microsoft\Windows\CurrentVersion\Extensions'
$key = Get-Item $path
$key
$key | Get-Member
Since $key is a Microsoft.Win32.RegistryKey, you cannot get the name value pairs out directly.
The first step is to create a list of PSCustomObjects with Name/Value pairs. The Name comes from the GetValueNames piped through a ForEach-Object. For each of those Names, we get the Value through GetValue:
$namevalues = $key.GetValueNames() |
ForEach-Object {
[PSCustomObject] #{
Name = $_;
Value = $key.GetValue($_)
}
}
$namevalues | Format-Table
An alternative for the first step is to use the Select-Object using -ExpandProperty as explained by Scott Saad:
$namevalues = $key | Select-Object -ExpandProperty Property |
ForEach-Object {
[PSCustomObject] #{
Name = $_;
Value = $key.GetValue($_)
}
}
$namevalues | Format-Table
The second step is to filter the $namevalues either by Name or by Value.
The Where-Object has some pretty cool Comparison operators that accept regular expressions like match, notMatch, etc.
To make the code more readable, you can wrap lines (thanks Joey!) either use the backtick (`) or take advantage of the places in the PowerShell syntax where it does accept line breaks, like after a pipe (|) or opening brace ({):
$matches = $namevalues |
Where-Object {
$_.Name -match '^xls' `
-or $_.Value -match 'msaccess.exe$'
}
$matches | Format-Table
The result is as wanted:
Name Value
---- -----
xlsx C:\PROGRA~2\MICROS~1\Office15\EXCEL.EXE
xls C:\PROGRA~2\MICROS~1\Office15\EXCEL.EXE
mdb C:\PROGRA~2\MICROS~1\Office15\MSACCESS.EXE
mda C:\PROGRA~2\MICROS~1\Office15\MSACCESS.EXE
There's a much more clever way to enumerate registry values (found it here). And it's more powershell-way IMO.
I've turned it into a one-liner:
(Get-ItemProperty $path).psobject.properties |
where {$_.name -like "xls*" -or $_.value -like "*\MSACCESS.EXE"} |
select name,value
Update: As noted in comments by #mklement0, you should be careful about properties PSPath, PSParentPath, PSChildName, PSDrive, and PSProvider within the psobject.properties.
A more robust PSv4+ alternative to montonero's helpful answer:[1]
Get-Item $path -PipelineVariable key | ForEach-Object Property | ForEach-Object {
$name = ($_, '')[$_ -eq '(default)'] # translate '(default)' to '' for API calls
if ($name -like 'xls*' -or ($value = $key.GetValue($name)) -like "*\MSACCESS.EXE")
{ [pscustomobject] #{ Name = $name; Value = $value } }
}
-PipelineVariable key stores the [Microsoft.Win32.RegistryKey] instance returned by Get-Item in variable $key for later use in the pipeline.
ForEach-Object Property enumerates the target key's value names (via the .Property note property that PowerShell adds to the output [Microsoft.Win32.RegistryKey] instance).
Inside the Where-Object script block, $_ then refers to the value name at hand, and $key.GetValue(<valueName>) is used to retrieve the associated data.
Important: In the .Property array, PowerShell translates the default value name, which is the empty string ('') at the API level, into name '(default)'; thus, if $_ is '(default)', you must translate it to '' before calling $_.GetValue(<valueName>), which is what ($_, '')[$_ -eq '(default)'] does.
[pscustomobject] #{ ... } then constructs and outputs a [pscustomobject] instance with .Name and .Value properties reflecting the matching value's name and data.
[1] montonero's answer is concise and works well in the case at hand, but it comes with caveats:
PowerShell's registry provider automatically adds the following additional note properties (members of type NoteProperty, as reflected in the output from Get-Member) containing metadata about the targeted registry keys to the [pscustomobject] instance that Get-ItemProperty outputs:
PSPath, PSParentPath, PSChildName, PSDrive, PSProvider
These can interfere with filtering based on .psobject.properties in two ways:
Using wildcard matching against $_.Name can accidentally include these properties.
E.g., $_.Name -like '*drive*' would match the PSDrive property, even though it isn't actually part of the registry key.
Perhaps more hypothetically, if the registry key happens to have values of the same name as these provider properties, the provider properties shadow (override) these values.
E.g., if the key has a PSPath value, $_.Value will report the provider property value instead.