tags:

views:

2454

answers:

4

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?

A: 

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
}
Darren Gosbell
A: 

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"}
Scott Weinstein
This solves an unrelated problem which I don't have. I didn't know about -match though, very cool thanks
Orion Edwards
+3  A: 

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.

Mike
ls blha blah | select name did it :-)
Orion Edwards
format-table -a also works. Thanks lots!
Orion Edwards
BTW, the reason for this is the default output formatting for FileInfo and DirectoryInfo objects. Using Select-Object or Format-Table specifies the output specifically.
JasonMArcher
+2  A: 

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

Marco Shaw