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
Related
So far I've got this:
Get-ChildItem -Recurse 'C:\MyFolder' |
Select-Object FullName, name |
Export-Csv -path 'C:\output.csv' -noTypeInfo
It gives me a CSV with the full path name, and name of each folder & file in a directory.
However, I'd like a 3rd column that has either 'Folder' or 'File' (or something similar). Basically just a column that explicitly tells me whether something is a file or folder.
I feel like it should be a simple case of adding a new column like FullName, name, Type - but not sure what options are available.
Is this possible?
You could use the Attributes property however, this might give you more information than you really need, see FileAttributes Enum.
If you simply need a property with 2 values (File or Directory) you can use the boolean property PSIsContainer as reference to construct your new calculated property:
Get-ChildItem -Recurse 'C:\MyFolder' |
Select-Object FullName, Name, #{N='Type';E={('File', 'Directory')[$_.PSIsContainer]}} |
Export-Csv -path 'C:\output.csv' -NoTypeInformation
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
I'm a complete beginner to Powershell and scripting, and have been successfully been using Out-GridView to display some properties of the files I have in my directories using the following:
dir D:\Folder1\$type -Recurse | Select Fullname,Directory,LastWriteTime | out-gridview
where I specifiy the file extension with $type = "*.pdf" for instance.
I would also like to start comparing files using hashcodes so I have tried this command:
ls | Get-Filehash
However, I would like to have the hashcodes in the output window as a seperate column with out-gridview. Is this possible? I've tried
dir D:\Folder1\$type -Recurse | Select Fullname,Directory,LastWriteTime,Filehash | out-gridview
and
dir D:\Folder1\$type -Recurse | Select Fullname,Directory,LastWriteTime | Get-Filehash | out-gridview
Of course neither of these work.
Does anyone have a way of generating hashcodes for a specific file extension only?
Many thanks in advance!
You can do this by using a calculated property with Select-Object:
Get-ChildItem -Path 'D:\Folder1\$type'-Recurse |
Select-Object FullName,Directory,LastWriteTime, #{Label='FileHash'; Expression={(Get-Filehash -Path $_.FullName).Hash}} |
Out-GridView
You should see a new column in the grid view called 'Filehash' that contains the SHA256 hash of the file. You can chage the algorithm (to, say, MD5) using the -Algorithm parameter of Get-FileHash.
If you're wondering what this is doing, the important parts are:
#{...}
signifies a hashtable. e.g. a set of key-value pairs
label
is the key that defines what your property (column name) will be in the grid view
expression defines the code snippet ({...}) that calculates the value of this property
$_
signifies that we are working with the 'current' object (file in this case) passing along the pipeline
I need to delete a registry entry based on its value. For example, the key will reside in:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
The DisplayName key will contain StackOverFlow.
The names of these registry keys are GUID, therefore random, so I can't just search for them, the only way is the one above.
$path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
gci $path |? {$_.GetValue('DisplayName') -match 'StackOverflow'} | del
I have a directory full of file pairs. Each pair of files have the same name with the extensions of mp3 and cdg (karaoke files!). I would like to use powershell to get the list of all distinct file names with no extensions
I've gotten as far as:
dir -recurse -filter "*.mp3" | select-object Name | sort
But I can't quite figure out how to pass each Name to [System.IO.Path]::GetFileNameWithoutExtension
how would I do this?
What you're looking for is the for-each (%) filter (not precisely sure if it's a filter or a cmdlet but it has the same usage syntax).
Try the following
dir -recurse -filter "*.mp3" |
%{ $_.Name } |
%{ [IO::Path]::GetFileNameWithoutExtension($_) } |
sort
EDIT Update
I changed my answer from "select-object Name" to "%{ $_.Name}". The former essentially takes the Name property off of the pipeline value and creates a new object with a single property of the specified name with the value on the original object. The latter will process every value in the pipeline and pass the result of executing $_.Name down the pipeline.
dir -recurse -filter "*.mp3"| select #{name='Name';Expression={[System.IO.Path]::GetFileNameWithoutExtension($_.Name)}} | sort
If you hate typing %{$_.foo} all the time like I do, try Get-PropertyValue (alias: gpv) from PSCX.
More musings here for the suitably geeky: http://richardberg.net/blog/?p=55
Now that PowerShell v2 is RTMd, you can select the BaseName member:
dir -recurse -filter *.mp3 | select BaseName | sort