Powershell : How to get a List of the File Names in a Folder using Powershell


When there is situation in Windows, there has to be some solution using powershell. So Once again I’ve something useful for you.

SITUATION : A folder in my Hard disk has about 300 movies and I want to share this list of movie names with my friend, or I want to compare my list with one of my friend’s collection of movies on his hard disk.

Below script will work in above situation, you just need to change few parameters in the script :

$list = Get-ChildItem -Path 'D:\Movies\' -Recurse |
?{$_.PSIsContainer -eq $false -and $_.Extension -ne '.srt'}
write-host "`nTotal : "$list.Count "movies `n"
ForEach($n in $list)
{
 $n.Name | Out-File -Append 'D:\Movielist.txt'
}

First highlighted parameter is the Directory of the folder, where you want the script to search for movie files, and the second highlighted one is the location of output file to be placed with name Movielist.txt like below. txt

If you want to compare two folders for files that aren’t common, for exmaple – I want to compare movies in my laptop with movies in my friends Hard Disk,just add the following  script in the above script and modify the highlighted location with directory to be compared.

$list2 = Get-ChildItem 'F:\FolderInFriendsHDD\'|
?{$_.PSIsContainer -eq $false -and $_.Extension -ne '.srt'}

Compare-Object -ReferenceObject $list -DifferenceObject $list2 -IncludeEqual

Your output will look something like this :

text

Happy Learning ! 🙂

Advertisements

2 thoughts on “Powershell : How to get a List of the File Names in a Folder using Powershell

  1. can you please help me in writing a script for copying files from one server to another server ? like we can login to a jump server and copy files from within the jump server

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s