When using powershell, sometimes I want to only display for example the name or fullname of a file.
From what I can gather, the way to do this is by using Get-ItemProperty
(alias of gp
) and passing -n fullname
, For example
PS C:\Dev> gp . -n fullname
Notably I want to use this in longer scripts combined with foreach
and where
, and so on
Powershell then displays the fullname, but it also displays a bunch of other stuff, as follows:
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Dev
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\
PSChildName : Dev
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
fullname : C:\Dev
My question is, how can I get powershell to only display the property I want (fullname) and not cruft the display up with all the other stuff. Is Get-ItemProperty
even the right way to do this?
If I do this:
ls -r | ?{ $_.fullname -match "foo" }
This gives me a series of lists, one for each directory, showing all the 'foo' files in each directory. What I'd like to do is consolidate those multiple lists into one single list, and not show the Mode
, LastWriteTime
, Length
or any other irrelevant stuff. If Get-ItemProperty
is not the correct way to show those things, what is?
You can use the cmdlet to establish or change the properties of items. For example, you can use Set-ItemProperty to set the value of the IsReadOnly property of a file object to $True . You also use Set-ItemProperty to create and change registry values and data.
Description. The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array. To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters.
Use Get-Member to see an object's properties and methods The Get-Member cmdlet is used to definitively show us a PowerShell object's defined properties and methods. We use it by piping the output from our Get-Service cmdlet into Get-Member.
The Get-ItemProperty cmdlet gets the properties of the specified items. For example, you can use this cmdlet to get the value of the LastAccessTime property of a file object. You can also use this cmdlet to view registry entries and their values.
There are many ways. I think you were looking for Get-ChildItem:
PS > get-childitem x.txt | select fullname
FullName
--------
C:\WINDOWS\system32\WindowsPowerShell\v1.0\x.txt
PS > get-childitem x.txt | select name
Name
----
x.txt
PS > get-childitem x.txt | % { $_.fullname }
C:\WINDOWS\system32\WindowsPowerShell\v1.0\x.txt
PS >
You could use Get-ItemProperty, but I think it's more for things like registries.
PS > Get-ItemProperty x.txt | select name
Name
----
x.txt
Format-Table, alias ft is also good to know about...
PS > gci x.txt | format-table -a fullname,length
FullName Length
-------- ------
C:\WINDOWS\system32\WindowsPowerShell\v1.0\x.txt 126
Commonly used aliases for Get-ChildItem, are ls, dir, and gci. I'd go with gci, when in Rome.
Just an additional comment... The way PowerShell displays information to the screen can also be modified by making changes to its Extended Type System (ETS).
Check the help for Update-TypeData.
Going this route does introduce some more advanced topics...
you can use (depending on what you exactly need):
(gi c:\temp\file -ea 0).fullname
(gi c:\temp\file -ea 0) | select -ExpandProperty fullname
C:\temp\file
-ea: do not show error if file does not exist
If you are in a foreach loop and you have a reference to the object that you are interested in, then have you tried something like the following to just echo the property.
foreach ($file in dir)
{
$file.fullname
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With