Home » PowerShell » PowerShell Get-ChildItem – Get Full Path of Files in Directory

PowerShell Get-ChildItem – Get Full Path of Files in Directory

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.

In this article, we will discuss different ways to get the full path of files in the folder using the Get-ChildItem cmdlet in PowerShell.

PowerShell Get-ChildItem – Get File Full Path

Use the Get-ChildItem cmdlet in the PowerShell to get the full path of the file. Get-ChildItem cmdlet takes folder path as input and uses the Filter parameter to search for the .py extension files in the directory.

It returns the files and passes the output to the next command to get the file fule path using the FullName property.

Get-ChildItem -Path D:\LogTest\FTP-02\ -Filter *.py -Recurse | %{$_.FullName}

In the above PowerShell script, Get-ChildItem uses a specified folder path and recurse parameter to get the file and passes the output to get the file’s full path using $_.FullName.

The output of the above script returns the files with a full path.

PowerShell Get-ChildItem - Get Full path for File
PowerShell Get-ChildItem – Get Full path for File

Cool Tip: How to get the file extension using PowerShell!

Get Full Path of File using ForEach-Object

To get the full path of the file in PowerShell, use the Get-ChildItem to get files in the directory and pass the output to foreach-object to iterate over the file and get the full name of the file.

Get-ChildItem -Path D:\LogTest\FTP-02\ -Filter *.py -Recurse  | ForEach-Object{$_.FullName} 

In the above PowerShell script, Get-ChildItem recursively gets the files from the specified location and passes them to ForEach-Object to get the full path of the file in the folder.

The output of the above script is:

PS D:\> Get-ChildItem -Path D:\LogTest\FTP-02\ -Filter *.py -Recurse  | ForEach-Object{$_.FullName}                     D:\LogTest\FTP-02\get-log.py
D:\LogTest\FTP-02\srtp.py
PS D:\>                                                                                                                          

Use Select-Object to Get Full Path of the File

Use the Get-ChildItem cmdlet in PowerShell to get the files in the folder using the file filter and using the Select-Object cmdlet to get the full path of the file using ExpandProperty FullName.

Get-ChildItem -Path D:\LogTest\FTP-02\ -File -Recurse | Select-Object -ExpandProperty FullName

In the above PowerShell script, Get-ChildItem uses the File filter to get only files from the directory recursively and passes them to the Select-Object cmdlet.

It uses ExpanProperty FullName to get the full path of the file.

The output for the above script is:

PS D:\> Get-ChildItem -Path D:\LogTest\FTP-02\ -File -Recurse | Select-Object -ExpandProperty FullName     

D:\LogTest\FTP-02\get-log.py
D:\LogTest\FTP-02\Newline-FileTest.txt
D:\LogTest\FTP-02\recent-file.txt
D:\LogTest\FTP-02\srtp.py
PS D:\> 

PowerShell Get-ChildItem Full Path of Files using Format-List

Using the Format-List cmdlet in the PowerShell, you can get the file’s full path.

Get-ChildItem -Path D:\LogTest\FTP-02\ -File -Recurse | Format-List FullName

In the above PowerShell script, Get-ChildItem gets files and passes them to the Format-List cmdlet to get the file path using the FullName property.

The output of the above script is:

PS D:\>  Get-ChildItem -Path D:\LogTest\FTP-02\ -File -Recurse | Format-List FullName                                   

FullName : D:\LogTest\FTP-02\get-log.py

FullName : D:\LogTest\FTP-02\Newline-FileTest.txt

FullName : D:\LogTest\FTP-02\recent-file.txt

FullName : D:\LogTest\FTP-02\srtp.py



PS D:\>  

Cool Tip: How to get file owner using PowerShell!

Conclusion

I hope the above article on how to use Get-ChildItem cmdlet in PowerShell to get the full path of files in the directory is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment