How do I do 'dir /s /b' in PowerShell?

后端 未结 7 570
梦毁少年i
梦毁少年i 2020-12-23 19:34

I have a folder with three files and want the equivalent of dir /s /b in PowerShell. How do I do that?

For example, if the folder name is temp3

7条回答
  •  执笔经年
    2020-12-23 20:16

    You can use

    Get-ChildItem -Recurse | Select-Object -ExpandProperty FullName
    gci -r | select -exp FullName
    

    or

    Get-ChildItem -Recurse | ForEach-Object { $_.FullName }
    gci -r | % { $_.FullName }
    gci -r | % FullName    # In recent PowerShell versions
    

    (The long version is the first one and the one shortened using aliases and short parameter names is the second, if it's not obvious. In scripts I'd suggest using always the long version since it's much less likely to clash somewhere.)

    Re-reading your question, if all you want to accomplish with dir /s /b is to output the full paths of the files in the current directory, then you can drop the -Recurse parameter here.

    My advice to you, though: Don't use strings when you can help it. If you want to pass around files, then just take the FileInfo object you get from Get-ChildItem. The cmdlets know what to do with it. Using strings for things where objects work better just gets you into weird problems.

提交回复
热议问题