Dhiwakar Ravikumar
Dhiwakar Ravikumar

Reputation: 2207

Formatting command output in Powershell

I'm trying to get the "dir" output for a given path using Powershell but I'm having a hard time trying to get the desired format. I'm new to Powershell so any suggestions on the command that I've used will also be very helpful to me.

Command used

 $dirs  = (Get-ChildItem -Recurse $path | Format-Table -HideTableHeaders | Out-String).Split("`n")

Output I get

    Directory: D:\GetDataTest





d-----         3/4/2018   6:02 PM                dir1                                                             

d-----         3/4/2018   6:02 PM                dir2                                                             

-a----         3/2/2018   3:56 PM           1024 file_1                                                           

-a----         3/2/2018   3:56 PM           1024 file_2                                                           





    Directory: D:\GetDataTest\dir1





-a----         3/2/2018   3:56 PM           1024 file_1                                                           

-a----         3/2/2018   3:56 PM           1024 file_2                                                           





    Directory: D:\GetDataTest\dir2





-a----         3/2/2018   3:56 PM           1024 file_1                                                           

-a----         3/2/2018   3:56 PM           1024 file_2                 

I would like get rid of all the whitespaces as well as the lines that read "Directory: .." before the list of items inside the directory.

The output format that I'm after is

d-----         3/4/2018   6:02 PM                dir1                                                             
d-----         3/4/2018   6:02 PM                dir2                                                             
-a----         3/2/2018   3:56 PM           1024 file_1                                                           
-a----         3/2/2018   3:56 PM           1024 file_2                                                           
-a----         3/2/2018   3:56 PM           1024 file_1                                                           
-a----         3/2/2018   3:56 PM           1024 file_2                                                           
-a----         3/2/2018   3:56 PM           1024 file_1                                                           
-a----         3/2/2018   3:56 PM           1024 file_2 

Upvotes: 2

Views: 639

Answers (1)

G42
G42

Reputation: 10019

As per JoesfZ's comment, you can specify the properties using Select-Object:

Get-Childitem -R $path | Select-Object -Property Mode, LastWriteTime, Length, Name

You can also manipulate the properties using a Name and Expression hashtable, as documented in the link above - for example, to strip out "D:\GetDataTest" from the FullName property:

Get-Childitem -R $path |
    Select-Object -Property @{Name = "PartialPath"; Expression = {($_.FullName).Replace("D:\GetDataTest","")}}

Name and Expression can be further abbreviated to n and e

Upvotes: 3

Related Questions