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.
To get folder name only in PowerShell, use Get-ChildItem – Directory parameter and select Name property to list folder name only on PowerShell console.
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.
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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With