Table of Contents
Get Full Path of File in PowerShell
To get full path of file in powershell:
- Go to folder where file is present. You can go to any parent folder as well.
- Use
Get-ChildItem
with-Recurse
option. - Pipe it to
where-object
cmdlet to filter filename. In this example, we have given filename astest.ps1
- Pipe it to forEach-Object cmdlet and get
FullName
property of current object.
1 2 3 |
Get-ChildItem -Recurse |Where-Object {($_.name -eq "test.ps1")}| %{$_.FullName} |
1 2 3 |
D:\PowerShell\test.ps1 |
Here are multiple examples to get Full Path of Files in directory in PowerShell. Each option will use different combinations with Get-ChildItem
cmdlet.
Using Get-ChildItem
Cmdlet:
To get the full path of files in directory, use the Get-ChildItem
cmdlet in PowerShell.
1 2 3 |
Get-ChildItem -Path "D:\PowerShell\Testing" -Recurse | %{$_.FullName} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
D:\PowerShell\Testing\important D:\PowerShell\Testing\a.py D:\PowerShell\Testing\abc.txt D:\PowerShell\Testing\dummy.txt D:\PowerShell\Testing\hello.py D:\PowerShell\Testing\How neuroencoding improves performance-1.docx D:\PowerShell\Testing\myApp.py D:\PowerShell\Testing\myApp2.py D:\PowerShell\Testing\my_dict.json D:\PowerShell\Testing\To do.txt D:\PowerShell\Testing\important\alexa.txt D:\PowerShell\Testing\important\django.py D:\PowerShell\Testing\important\neuro.py D:\PowerShell\Testing\important\python.txt D:\PowerShell\Testing\important\result.docx |
In the above code, the Get-ChildItem
cmdlet was used to get all the full names of the files available in the D:\PowerShell\Testing directory and its subdirectory. Here, the -Path
parameter may specify the directory path from which we want to get the path of the files.
Let’s see all the parameters used in the above command one by one to understand it:
- The
-Recurse
parameter informs the PowerShell to recursively search the D:\PowerShell\Testing directory and all its subdirectories. |
is called a pipeline operator; it can pass the previous command’s output as the input to the next command. In this example, it passed all the names of the files in the D:\PowerShell\Testing directory to this%{$_.FullName}
command.%
is used as an alias of theForEach-Object
cmdlet{$_.FullName}
shows the action we want to take on the selected object. As the name suggests, we want to get the full name of all the files in the specified directory.
Now let’s consider a scenario in which we must extract all the .txt
files from the specified directory. For this, look at the below example:
1 2 3 |
Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.txt -Recurse | %{$_.FullName} |
1 2 3 4 5 6 7 |
D:\PowerShell\Testing\abc.txt D:\PowerShell\Testing\dummy.txt D:\PowerShell\Testing\To do.txt D:\PowerShell\Testing\important\alexa.txt D:\PowerShell\Testing\important\python.txt |
In this example, the -Filter
parameter is used with the Get-ChildItem
cmdlet to filter all the .txt
files available in the directory D:\PowerShell\Testing and its subdirectories.
To get full path of file in current directory, use Get-ChildItem
without path attribute.
1 2 3 |
Get-ChildItem -Recurse | %{$_.FullName} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
D:\PowerShell\Testing\important D:\PowerShell\Testing\a.py D:\PowerShell\Testing\abc.txt D:\PowerShell\Testing\dummy.txt D:\PowerShell\Testing\hello.py D:\PowerShell\Testing\How neuroencoding improves performance-1.docx D:\PowerShell\Testing\myApp.py D:\PowerShell\Testing\myApp2.py D:\PowerShell\Testing\my_dict.json D:\PowerShell\Testing\To do.txt D:\PowerShell\Testing\important\alexa.txt D:\PowerShell\Testing\important\django.py D:\PowerShell\Testing\important\neuro.py D:\PowerShell\Testing\important\python.txt D:\PowerShell\Testing\important\result.docx |
Using ForEach-Object
Cmdlet with Get-ChildItem
To get the full path of files in a directory, use the Get-ChildItem
cmdlet with the ForEach-Object
cmdlet in PowerShell.
1 2 3 |
Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.py -Recurse | ForEach-Object{$_.FullName} |
1 2 3 4 5 6 7 8 |
D:\PowerShell\Testing\a.py D:\PowerShell\Testing\hello.py D:\PowerShell\Testing\myApp.py D:\PowerShell\Testing\myApp2.py D:\PowerShell\Testing\important\django.py D:\PowerShell\Testing\important\neuro.py |
In this example, the Get-ChildItem
was used with the ForEach-Object
to filter all the files with the .py
extension available in the specified folder and its sub-folders. Here the Get-ChildItem
recursively got the names of all files and passed them to the ForEach-Object
cmdlet to get the full path of each file in the directory.
Suppose you want to get the full path of the files with the size of files in the given directory and its sub-directories.
1 2 3 |
Get-ChildItem -Path "D:\PowerShell\Testing" -Filter "*.txt" | ForEach-Object { $_.FullName + " has a size of " + $_.Length + " bytes." } |
1 2 3 4 5 |
D:\PowerShell\Testing\abc.txt has a size of 814 bytes. D:\PowerShell\Testing\dummy.txt has a size of 406 bytes. D:\PowerShell\Testing\To do.txt has a size of 129 bytes. |
We can observe the path of all files having the .txt
extension with their size. Here, the $_.Length
parameter was used to get the length/size of each file.
Using Select-Object
Cmdlet with Get-ChildItem
To retrieve the full path of files in a directory, use the Get-ChildItem
with the Select-Object
cmdlet in PowerShell.
1 2 3 |
Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.py -Recurse | Select-Object -ExpandProperty FullName |
1 2 3 4 5 6 7 8 9 10 |
FullName -------- D:\PowerShell\Testing\a.py D:\PowerShell\Testing\hello.py D:\PowerShell\Testing\myApp.py D:\PowerShell\Testing\myApp2.py D:\PowerShell\Testing\important\django.py D:\PowerShell\Testing\important\neuro.py |
In the above command, Select-Object
was used with Get-ChildItem
. It is another way to get the path of files available in the specified directory. Here, Select-Object
was used to select the object’s specific property, which was ExpandProperty FullName
in our case. This was used to expand the FullName
property of the object to get the full path of the files available in the given directory and its subdirectories which was D:\PowerShell\Testing in the above example.
Using Format-List
Cmdlet with Get-ChildItem
To access the full path of files in a directory, use the Get-ChildItem
cmdlet with Format-List
in PowerShell.
1 2 3 |
Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.txt -Recurse | Format-List FullName |
1 2 3 4 5 6 7 |
FullName : D:\PowerShell\Testing\abc.txt FullName : D:\PowerShell\Testing\dummy.txt FullName : D:\PowerShell\Testing\To do.txt FullName : D:\PowerShell\Testing\important\alexa.txt FullName : D:\PowerShell\Testing\important\python.txt |
The Format-List
cmdlet was used in the above command to get the output as a list. The above example got the path of the files with the .txt
extension available in the D:\PowerShell\Testing directory and its subdirectories. The above output is formatted and written FullName:
at the start of each file path.
Let’s see another example to get all the files with the .docx
extension.
1 2 3 |
Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.docx -Recurse | Format-List FullName |
1 2 3 4 |
FullName : D:\PowerShell\Testing\How neuroencoding improves performance-1.docx FullName : D:\PowerShell\Testing\important\result.docx |
This code is similar to the previous one. However, this time, we got the path of all files with the .docx
extension.
Using Format-Table
Cmdlet with Get-ChildItem
To get the files’ full path in a directory, use the Get-ChildItem
cmdlet with the Format-Table
cmdlet in PowerShell.
1 2 3 |
Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.txt -Recurse | Format-Table FullName -AutoSize |
1 2 3 4 5 6 7 8 9 |
FullName -------- D:\PowerShell\Testing\abc.txt D:\PowerShell\Testing\dummy.txt D:\PowerShell\Testing\To do.txt D:\PowerShell\Testing\important\alexa.txt D:\PowerShell\Testing\important\python.txt |
In the above PowerShell snippet, the Format-Table
cmdlet was used along with the Get-ChildItem
cmdlet to get the full path of the files with the .txt
extension available in the specified directory and its subdirectory. Due to using the Format-Table
cmdlet, the output was formatted as a table with the complete path to each file displayed. And the -AutoSize
parameter was used to adjust the column’s width to fit the data.
That’s all about how to get full path of file in PowerShell.