Share via


How to get folder name using get-childitem

Question

Thursday, March 23, 2017 10:46 AM

I want to display the folder name and using following code

$homeFolders = Get-ChildItem -LiteralPath $folder123 -Force -ErrorAction SilentlyContinue| select FullName, Name, PSIsContainer, Length, Extension
foreach($folder in $homeFolders){
 write-host $folder.FullName
 write-host "$folder.Name
}

It is display actual folder name, it is displaying as
\server\user1\Home\test1
@{FullName=\server\user1\Home\test1; Name=test1; PSIsContainer=True; Length=; Extension=}.Name is FOLDER

I want to folder name should display as test1.

Pls let me know what I am missing here

Albert

All replies (5)

Thursday, March 23, 2017 11:26 AM ✅Answered

$(folder.fullname | split-path -leaf)


Thursday, March 23, 2017 11:26 AM ✅Answered

write-host "$($folder.FullName)"
write-host "$($folder.Name)"

Grüße - Best regards

PS:> (79,108,97,102|%{[char]$_})-join''


Thursday, March 23, 2017 1:32 PM

Just remove the extra quote.

dir -Directory | %{Write-Host $_.Name}

\(ツ)_/


Thursday, March 23, 2017 1:54 PM

Hello BOFH_666/Mekas,

Thanks for solution. I have one more question

If I have to display like "Home\test1", how can I do this?


Thursday, March 23, 2017 2:23 PM

quick and dirty:

'\\server\user1\Home\test1' -Split  '\\\\server\\user1\\'

Grüße - Best regards

PS:> (79,108,97,102|%{[char]$_})-join''