Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Get-ChildItem to retrieve folder,name,fullname

Tags:

powershell

I'm currently using this script to pull the Name,Folder,Foldername from a given path:

   Get-ChildItem "C:\user\desktop"  | Select Name, `
  @{ n = 'Folder'; e = { Convert-Path $_.PSParentPath } }, `
  @{ n = 'Foldername'; e = { ($_.PSPath -split '[\\]')[-2] } } ,
  @{ n = 'Fullname'; e = { Convert-Path $_.PSParentPath } } |
    Export-Csv "C:\user\desktop\txt.txt" -Encoding Utf8 -NoTypeInformation

I am having trouble getting @{ n = 'Fullname'; e = { Convert-Path $_.PSParentPath } } to pull through the full file path.

Any help greatly appreciated.

like image 505
Carlos Avatar asked Oct 13 '16 23:10

Carlos


People also ask

How do I get only the directory name in PowerShell?

To get folder name only in PowerShell, use Get-ChildItem – Directory parameter and select Name property to list folder name only on PowerShell console.

What is the use of Get-ChildItem?

The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.

What does Get-ChildItem do in PowerShell?

Get-ChildItem displays the files and directories in the PowerShell console. By default, Get-ChildItem lists the mode (Attributes), LastWriteTime, file size (Length), and the Name of the item. The letters in the Mode property can be interpreted as follows: l (link)

How do you get the full path of ChildItem?

Use the Get-ChildItem cmdlet in PowerShell to get the full path of the file in the current directory. Get-ChildItem returns one or more items from the specified location and using the file FullName property, it gets the full path of the file.


1 Answers

You mistakenly referenced PSParentPath when you meant PSPath to get the full name (full filesystem path):

Get-ChildItem "C:\user\desktop" | Select Name, `
  @{ n = 'Folder'; e = { Convert-Path $_.PSParentPath } }, `
  @{ n = 'Foldername'; e = { ($_.PSPath -split '[\\]')[-2] } } ,
  @{ n = 'Fullname'; e = { Convert-Path $_.PSPath } }  # NOT $_.PS*Parent*Path

However, as others have pointed out, the full path is a standard property on the output objects produced by Get-ChildItem, so you can simply reference the FullName property:

Get-ChildItem "C:\user\desktop" | Select Name, `
  @{ n = 'Folder'; e = { Convert-Path $_.PSParentPath } }, `
  @{ n = 'Foldername'; e = { ($_.PSPath -split '[\\]')[-2] } } ,
  FullName

P.S.: '\\' will do as the RHS of the -split operator, but if you wanted to be cross-platform-friendly, you could use [\\/]

Note: The hashtable-based technique used above (@{ n = ...; e = ... }, where n is short for Name and e for Expression) is called a calculated property, described in the conceptual about_Calculated_Properties help topic, across all cmdlets that support calculated properties.

like image 142
mklement0 Avatar answered Nov 09 '22 18:11

mklement0