简体   繁体   中英

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

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?

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.

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

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

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
}

I've always restricted my use of Get-ItemProperty for navigation providers like the registry where getting data via Get-Item or get-ChildItem. Compare

ls hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
gp hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

what you want to do is use the properties directly

ls | ? { $_.FullName -match "whatever"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM