I have a string variable pointing to a directory. In this directory is a single file with the extension .xyz
. How do I get the path to this file, without knowing the file name itself?
I have the following code so far:
$dir = "C:\Temp\MyDir"
$xyzFiles = @(Get-ChildItem "$dir\*.xyz")
$file = $xyzFiles[0]
Is this "the way to go" in PowerShell? Is there a better solution?
For finding a specific file type, simply use the 'type:' command, followed by the file extension. For example, you can find . docx files by searching 'type: . docx'.
To find all files with a file extension, write out its path to find a command with the options and expression specifying the extension. In the below-given example, we will find all files with the “. txt” extension.
In powerShell version 5, you can also try this:
Get-ChildItem $dir -Recurse -Include *.xyz
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