tt0686
tt0686

Reputation: 1849

Get Directory Name from FileInfo object

I was looking for a executable location and I use the following command:

PS C:\> dir -recurse -filter "notepad++.exe" -ErrorAction SilentlyContinue | select -first 1 -Property Directory

And it returns:

Directory
---------
C:\Program Files (x86)\OpenSource\Notepad++

How can I get the string "C:\Program Files (x86)\OpenSource\Notepad++"? I have try a lot of different commands but none of them work

Upvotes: 1

Views: 1011

Answers (2)

Cellcon
Cellcon

Reputation: 1295

I will aproach this step by step, a single line solution is presented at the end of the first part of my answer.


The Specific Solution for Your Case

Right now, you are receiving an object. One simple solution is to store the object in a variable and access the path of the directory as a string, by using the ToString() method.

$a = dir -recurse -filter "notepad++.exe" -ErrorAction SilentlyContinue | select -first 1 -Property Directory
$a.Directory.ToString()

In that case, you can also shorten your dir call like in the following snippet.

$a = dir -recurse -filter "notepad++.exe" -ErrorAction SilentlyContinue | select -first 1
$a.Directory.ToString()

Or use the DirectoryName property, like in Bacon Bits answer.

$a = dir -recurse -filter "notepad++.exe" -ErrorAction SilentlyContinue | select -first 1
$a.DirectoryName

And finally, you can do it in a single line, too.

dir -recurse -filter "notepad++.exe" -ErrorAction SilentlyContinue | select -first 1 | % { $_.Directory.ToString() }

Or, again, use the DirectoryName Property.

dir -recurse -filter "notepad++.exe" -ErrorAction SilentlyContinue | select -first 1 | % { $_.DirectoryName }

All of the solutions above will return the path as string, on your system that would be the following one.

C:\Program Files (x86)\OpenSource\Notepad++

The General Approach to the Solution

If you want to access any property of an object in PowerShell, you need to know the available properties first. Just pipe your object to the Get-Member Cmdlet and PowerShell will print you the TypeName and a table of property names, types and definitions.

dir -recurse -filter "notepad++.exe" -ErrorAction SilentlyContinue | select -first 1 | Get-Member

A quick web research would deliver even more information about the type of your object.

When you know which property you want to use, you may access it by using one of the following methods.

  1. Store the returned object (e.g. as a) and access the property by using $a.PropertyName
  2. Pipe the object to % { $_.PropertyName }
  3. Use -ExpandProperty PropertyName, like Bacon Bits did.

Just use the style that fitrs best to your remaining code.


Difference Between -Property and -ExpandProperty

-Property will provide you with an object, containing exactly this property.

-ExpandProperty will return the property itself.

Upvotes: 2

Bacon Bits
Bacon Bits

Reputation: 32200

Use the -ExpandProperty parameter of Select-Object, and ask for the DirectoryName property instead of the Directory property:

dir -recurse -filter "notepad++.exe" -ErrorAction SilentlyContinue | select -first 1 -ExpandProperty DirectoryName

Upvotes: 1

Related Questions