I'm trying to get the Program IDs of DCOM applications but when returning the value of the property, it doesn't give the full contents.
$a = Get-ChildItem 'registry::HKEY_CLASSES_ROOT\WOW6432Node\CLSID\' -Recurse | Where-Object Name -match 'ProgID'
This returns all applications that contain a ProgID
Hive: HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{000C1090-0000-0000-C000-000000000046}
Name Property
---- --------
ProgId (default) : WindowsInstaller.Installer
When trying to get the property value in the example, "WindowsInstaller.Installer"
via $a.Property
returns
(default)
How do I return the full property contents?
What Get-ChildItem returns for registry locations are Microsoft.Win32.RegistryKey instances representing registry keys.
To get the data for the unnamed default value of each such key - which contains the ProgID of interest - you can use the .GetValue() method with the empty string ('').
Note that PowerShell represents the unnamed default value differently, as '(default)', as shown in your question.
Get-ChildItem registry::HKEY_CLASSES_ROOT\WOW6432Node\CLSID -Recurse |
Where-Object Name -match ProgID |
ForEach GetValue ''
As an aside: the Name property of registry [Microsoft.Win32.RegistryKey] instances contains the full key path, so a better way to limit matching to just a key's name is to use
Where-Object Name -match '\\ProgID$ in your case.
Related
I can get a key in the Uninstalls key of the registry, for a specific installed program, like this...
$displayName = 'Parallels Tools'
$key = 'Registry::HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall'
Get-ChildItem -Path:$key | Where-Object {$_.GetValue('DisplayName') -eq $displayName}
... and the result will be a table, with a Name column showing just the GUID key name and a Properties column showing all the properties in the key. However, if I try to get just the GUID I run into problems. The column headers suggest (Get-ChildItem -Path:$key | Where-Object {$_.GetValue('DisplayName') -eq $displayName}).Name should work, but that shows the entire path, as does (Get-ChildItem -Path:$key | Where-Object {$_.GetValue('DisplayName') -eq $displayName}).ToString(). Is there some secret sauce happening with the dump to console? I know I can get to the GUID in a number of ways, Split-Path, or a Regex if I want to verify that the key is a GUID vs just a name. But I am curious if there is a way to leverage the same approach that dumping the object to the console is using?
Taking a look at the default formatter for RegistryKey objects, the Name column in the default view is populated using the .PSChildName property, which is the "leaf" of the full provider path.
The "parent" portion of the provider path is stored in the .PSParentPath property.
If I understand well your question you can try :
Get-ChildItem -Path:$key | Select-Object Name | Split-Path -Leaf
I'm trying to change the location setting of a network to Private regardless of whether it's connected or not, but the Get/Set-NetConnectionProfile cmdlet doesn't work for this function unless you're currently connected to the network you're trying to change.
Currently I'm able to search and list the GUIDs and all subkeys of the existing networks with
Get-ChildItem -path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles' -Recurse |
Where-Object { $_.GetValueNames() -match 'ProfileName' }
but I'm not sure how to chain that into just returning the Key with the ProfileName subkey = "foo". I plan to save the path to a variable and then change the "Category" subkey to private. I'm just not sure how to bridge the two pieces and just get the key/path of the network profile I want.
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles' |
Where-Object { $_.GetValue('ProfileName') -eq 'foo' }
$_.GetValue('ProfileName') looks for a value named ProfileName on each Microsoft.Win32.RegistryKey instance returned by the Get-ChildItem call and compares its data to string 'foo'; note that if a child key should happen not to have a ProfileName value, the .GetValue() call would quietly return nothing ($null).
As you later pointed out, piping to Set-ItemProperty allows modification of the values of the key returned; e.g.:
... | Set-ItemProperty -Name Category -Value 1
$loadbalancer = Get-AzureRmLoadBalancer | select Name,ResourceGroupName
$loadbalancer has two value ILB name and its resource group.
I want call both value in single PS command, Like when I call ILB name its respective resource group should be called.
The problem isn't entirely clear, can you please give an example of the function being used in the next step. I used "get-command" for the examples rather than get-azurermloadbalancer to avoid being package dependent.
To get the propety from $loadbalancer you use a dot and the name.
To get the current row from pipeline you can use $_
$loadbalancer = Get-Command get-* | Select-Object -Property CommandType,Name
#get a particular property
$loadbalancer.CommandType
#Use both properties in one function
$loadbalancer | Where-Object{($_.CommandType -eq "function") -and ($_.Name -match "Token")}
I'm parsing filenames in Powershell, and when I use Get-ChildItem | select name, I get a clean output of the files:
file1.txt
file2.txt
file3.txt
But when I try to narrow down those files with Select-String, I'm getting a weird # and { in front of my output:
Get-ChildItem | select name | Select-String -Pattern "1"
#{file1.txt}
Is there a parameter I'm missing? If I pipe with findstr rather than Select-String it works like a charm:
Get-ChildItem | select name | Findstr "1"
file1.txt
You can simplify and speed up your command as follows:
#((Get-ChildItem).Name) -match '1'
Note: #(), the array-subexpression operator, is needed to ensure that -match operates on an array, even if only one file happens to exist in the current dir.
(...).Name uses member-access enumeration to extract all Name property values from the file-info objects returned by Get-ChildItem.
-match, the regular-expression matching operator, due to operating on an array of values, returns the sub-array of matching values.
To make your original command work:
Get-ChildItem | select -ExpandProperty Name |
Select-String -Pattern "1" | select -ExpandProperty Line
select -ExpandProperty Name makes select (Select-Object) return only the Name property values; by default (implied -Property parameter), a custom object that has a Name property is returned.
select -ExpandProperty line similarly extracts the Line property value from the Microsoft.PowerShell.Commands.MatchInfo instances that Select-String outputs.
Note that in PowerShell [Core] v7+ you could omit this step by instead using Select-String's (new) -Raw switch to request string-only output.
As for what you tried:
As stated, by not using -ExpandProperty, select name (implied -Property parameter) created a custom object ([pscustomobject] instance) with a Name property.
Select-String stringifies its input objects, if necessary, so it can perform a string search on them, which results in the representation you saw; here's a simulation:
# Stringify a custom object via an expandable string ("...")
PS> "$([pscustomobject] #{ Name = 'file1.txt' })"
#{Name=file1.txt}
As an aside:
The above stringification method is essentially like calling .ToString() on the input objects[1], which often results in useless string representations (by default, just the type name); a more useful and intuitive stringification would be to use PowerShell's rich output-formatting system, i.e. to use the string representation you would see in the console; changing Select-String's behavior to do that is the subject of this feature request on GitHub.
[1] Calling .ToString() directly on a [pscustomobject] instance is actually still broken as of PowerShell Core 7.0.0-rc.2, due to this bug; the workaround is to call .psobject.ToString() or to use an expandable string, as shown above.
I can't seem to get my brain around something and I am hoping someone can help
I have an array of objects called $SnapVMsAll that looks like this:
VMName Name SnapshotType CreationTime ParentSnapshotName
------ ---- ---------- ------------ ------------------
SHARED-server.host.com SHARED-server.host.com - (02/10/2017 - 13:02:44) Standard 02/10/2017 13:05:58
I need to display all the records in this array of objects that have string "Veeam" in the name column, but I think I am having problems isolating a specific attribute of the object to compare.
My attempts have been as follows:
echo $SnapVMsAll | Where-Object (Foreach-Object -MemberName name) -Like "Veeam Replica"
This returned the error:
Where-Object : Cannot validate argument on parameter 'Property'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
I have also tried
echo $SnapVMsAll | Where-Object $SnapVMsAll.name -Like "Veeam Replica"
But again I get the same error.
$SnapVMsAll does not exist inside the pipeline, it is the object you pass to the pipeline. Within a pipeline, objects can be accessed using the automatic variable $_.
Also note that -Like is usually used with wildcards to match a string. If the name is Veeam Replica you should use -eq, if the name contains Veeam Replica, you should change to -Like "*Veeam Replica*"
So changing your code to the following should work:
$SnapVMsAll | Where-Object { $_.name -Like "*Veeam Replica*" }