2

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 
6
  • 1
    dir -R $path | Select-Object -Property Mode, LastWriteTime, Length, Name Commented Mar 4, 2018 at 12:50
  • Thanks a lot @JosefZ Commented Mar 4, 2018 at 12:55
  • 1
    At the conceptual level, you need to know that the output of GCI is not the same as the same output as displayed to the screen. The output of GCI is a stream of objects. What goes to the screen is a stream of characters. Very different. Commented Mar 4, 2018 at 13:18
  • @JosefZ, quick question, I tried using the property FullName but I'd like to remove the first level. Basically FullName for one of the files is D:\GetDataTest\dir2\file. I'd like to only print dir2\file2 and strip the first level i.e. strip D:\GetDataTest Commented Mar 4, 2018 at 13:19
  • To remove the top level directory, you could dir -R $path | Select-Object -Property Mode, LastWriteTime, Length, @{n='name';e={$_.FullName -replace '^.:\\[^\\]*\\(.*)','$1'}}. Commented Mar 4, 2018 at 13:44

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.