The following PowerShell one liners will help you in getting a list of files and/or folders in a given folder.
It can be useful to capture this information in a text file for later processing or even a spreadsheet.
PowerShell is very powerful in that it returns objects that you can manipulate.
For Example, to return the list of ONLY directories on the C:\ drive:
PS C:\> Get-ChildItem | where {$_.PsIsContainer}
Mode LastWriteTime Length Name
—- ————- —— —-
d—- 3/12/2009 12:28 PM Projects
d—- 6/10/2010 9:38 AM cygwin
d—- 9/21/2010 7:02 PM Documents and Settings
…
Get-ChildItem can be run with no arguments to get all items, just like a ‘dir’ command.
In fact, dir, ls, gci are aliases of the Get-ChildItem cmdlet.
In the following example, I will get the list of ONLY the directories and then from this I will take only the name. Also, I will use the alias ‘dir’
PS C:\> dir | where {$_.PsIsContainer} | Select-Object Name
Name
—-
Projects
cygwin
Documents and Settings
…
If you wish to get the list of ONLY files, you just need to negate the where condition:
where {!$_.PsIsContainer}
PS C:\> Get-ChildItem | where {!$_.PsIsContainer} | Select-Object Name
Name
—-
.rnd
AUTOEXEC.BAT
CONFIG.SYS
cygwin.lnk
install_log
You can redirect this output to a text file for later processing:
PS C:\> Get-ChildItem | where {!$_.PsIsContainer} | Select-Object Name > onlyFiles.txt
Now let’s take it one step further and send this output to a CSV(Comma Separated Values) file.
PS C:\> Get-ChildItem | where {!$_.PsIsContainer} | Select-Object Name | Export-Csv onlyFiles.csv
Comments
Leave a comment Trackback