PowerShell list all files in a directory.
To list all files in a directory is quite easy using PowerShell, as long as the path does not exceeds the path limitation.
Using the parameter "-Recurse" will include sub-folders on the path specified.
".FullName" option will include the path and the file name.
If ".FullName" is not specified, will list only the file name without the path.
If don't need to include files on sub folders omit or remove "-Recurse" option.
========================
$xfile = Get-ChildItem -Recurse -Path D:\MyMusicMP3
foreach ($filename in $xfile)
{
write-host $filename.FullName
}
$xfile = Get-ChildItem -Recurse -Path D:\MyMusicMP3foreach ($filename in $xfile)
{
write-host $filename
}
==========================
Script below will list only the files on the root directory specified; sub folder files will not be listed.
$xfile = Get-ChildItem -Path D:\MyFolder
foreach ($filename in $xfile)
{
write-host $filename.FullName
}
$xfile = Get-ChildItem -Path D:\MyFolder
foreach ($filename in $xfile)
{
write-host $filename
}
============================
To filter files or select specific files base on file name extension.
$xfile = Get-ChildItem -Path D:\MixFolder -Filter *.pdf
foreach ($filename in $xfile)
{
write-host $filename.FullName
}
Script above will filter or display only pdf files, just specify any file name extension to select or filter any specific files.
Or to select specific file names:
$xfile = Get-ChildItem -Path D:\MixFolder -Filter 2013*.pdf
foreach ($filename in $xfile)
{
write-host $filename.FullName
}
Script above will select any pdf files with file name that begins with "2013".
Cheers!!! Hope it helps..
Comments
Post a Comment