How can I list and print files in a directory?

Concerning your code in the question.

Your command should have worked as is. You are, in fact, already calling Get-ChildItem. If you check Get-Alias you will see what I'm trying to tell you.

PS C:\users\Cameron\Downloads> Get-Alias dir

CommandType     Name                                               ModuleName                                                                                               
-----------     ----                                               ----------                                                                                               
Alias           dir -> Get-ChildItem  

You code translates to

Get-ChildItem -Recurse "C:\temp" | Select Fullname

Again, I'm not sure why your code does not generate output since that is perfectly fine on a folder that contains files or directories. Might be an issue with the positional parameter maybe? What is your PowerShell version? ( Use Get-Host).

The code you have would send all file paths to console. Did you want that output somewhere else?

About the accepted answer

Pretty sure this code will double up output if you have folders in the path since directory will output to the second Get-ChildItem

Dir -Recurse c:\path\ | Get-Childitem

Consider the following folder tree

C:\TEMP\TEST
│   File1.txt
│   File2.txt
│
└───Folder1
        File3.txt

Consider the two command run against that folder tree.

PS C:\users\Cameron\Downloads> Dir -Recurse c:\temp\test | Select Fullname

FullName                                                                                                                                                                    
--------                                                                                                                                                                    
C:\temp\test\Folder1                                                                                                                                                        
C:\temp\test\File1.txt                                                                                                                                                      
C:\temp\test\File2.txt                                                                                                                                                      
C:\temp\test\Folder1\File3.txt                                                                                                                                              



PS C:\users\Cameron\Downloads> Dir -Recurse c:\temp\test | Get-Childitem | Select Fullname

FullName                                                                                                                                                                    
--------                                                                                                                                                                    
C:\temp\test\Folder1\File3.txt                                                                                                                                              
C:\temp\test\File1.txt                                                                                                                                                      
C:\temp\test\File2.txt                                                                                                                                                      
C:\temp\test\Folder1\File3.txt   

The second command shows two files called File3.txt when in reality there is only one.


Take a look at Get-Childitem

Dir -Recurse c:\path\ | Get-Childitem

get-childitem | format-list > filename.txt 

This will give you a text file with name, size, last modified, etc.

if you want specific parameters from the item... such as name of the file only the command is

get-childitem | format-list name > filename.txt

this is give you the same text file, but with just the name of the files listed.

Tags:

Powershell