Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Windows Powershell Get-ItemProperty to only show the property I want?

Tags:

powershell

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?

Update:

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?

like image 943
Orion Edwards Avatar asked Jan 18 '09 22:01

Orion Edwards


People also ask

How do I set properties in PowerShell?

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.

Which command will help in getting only selected properties of a command in PowerShell?

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.

Which key command in PowerShell shows an object's methods and properties?

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.

How do I get the properties of a file in PowerShell?

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.


4 Answers

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.

like image 89
Mike Avatar answered Oct 06 '22 05:10

Mike


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...

like image 23
Marco Shaw Avatar answered Oct 06 '22 04:10

Marco Shaw


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

like image 33
emekm Avatar answered Oct 06 '22 05:10

emekm


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
}
like image 31
Darren Gosbell Avatar answered Oct 06 '22 05:10

Darren Gosbell