If I run this simple command:
gci e:\mytree -r | select fullname
it gives this output:
E:\mytree\folder1
E:\mytree\folder2
E:\mytree\folder3
E:\mytree\file1.txt
E:\mytree\file12.txt
E:\mytree\folder1\folder.with.dots
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt
E:\mytree\folder3\file4.doc
Is there any way to include in the output even root folder, so that I could have
E:\mytree
E:\mytree\folder1
E:\mytree\folder2
E:\mytree\folder3
E:\mytree\file1.txt
E:\mytree\file12.txt
E:\mytree\folder1\folder.with.dots
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt
E:\mytree\folder3\file4.doc
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.
To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter. Gets files.
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.
More About Root Folders & Directories The same concept applies here as on your local computer—the files and folders in this root folder contain the main web page files, such as HTML files, that should be displayed when someone accesses the main URL of the website.
get-childitem $root -recurse | foreach-object -begin { $arr = @((get-item $root).fullname) } -process { $arr+= $_.fullname } -end { $arr }
Using foreach-object cmdlet's begin switch, we do some work before handling the objects from get-childitem: we create an array and put the filepath of the root in there.
Then for each object in the pipeline, we append its filepath to the array.
Finally, we output the array to the pipeline.
@(gi e:\mytree) + @(gci e:\mytree -r) | select fullname
@(..) forces the return value from each expression to be an array
Another option is to just roll a function such as below.
Function Get-ItemTree {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[ValidateScript({if (Test-Path $_ -PathType 'Container'){$true}else{throw "Invalid Path: '$_'. You must provide a valid directory."}})]
[String]$Path
)
Get-Item -Path $Path
Get-ChildItem -Path $Path -Recurse -OutBuffer 1000
}
You can add in any parameters you wish to pass through; such as a -Filter argument. Here I've hardcoded the assumption that if using this function you'd want it to be run on a container rather than leaf, you'd want to recurse, and may want to fetch a bunch of files at a time (giving a slight performance gain via the OutBuffer option).
It's not as sexy or clever as other solutions, but should be efficient, easy to reuse, easy to understand, and works for empty containers.
This is similar to your own answer, but has a few differences.
Write-Host
(a cmdlet you should generally avoid)Select-Object -Properties FullName
or Select-Object -ExpandProperty FullName
(the former creating the same output as your current function, the latter returning an array of strings holding the fullname value).Long term, I've suggest this Enhancement to MS.
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