Technology / System Admin

4 Best PowerShell Cmdlets for Managing Files

by Matt Snyder
4 Best PowerShell Cmdlets for Managing Files picture: A
Follow us
Published on October 18, 2021

In a Windows environment, you will eventually be faced with an extremely mundane task that involves files and folders. It may be searching through folders with thousands of files to search for specific types, moving files from one share to another, or even creating files in an automated fashion.

Either way, it would be extremely helpful to know this small group of PowerShell cmdlets and some of their more popular parameters in order to make your mundane tasks a little bit easier. Once you have a good grasp on these cmdlets, take a shot at our Windows Server training to learn even more.

The more you use them, the more familiar you will become and then maybe even explore some of the more complex parameters so as to become even more efficient in your day-to-day tasks. Let's take a look at these well-known cmdlets.

1. Listing Files & Folders

Get-ChildItem is the cmdlet used to get a listing of all the files and folders within a particular folder. At minimum, you at least need the -Path parameter value to point PowerShell to a folder to look into to list files and folders. To list the files and folders in the directory C:\Temp:

Get-ChildItem -Path "C:\Temp"

You will then get an immediate output of all the files and folders within the C:\Temp folder, with the default column headers of:

Mode     LastWriteTime     Length     Name
——     ——————-     ———     ——

Depending on the number of items in the folder, you will get either the short list displayed after the input or lines and lines of output will fly up your console window listing all the items available. By default, this cmdlet will not show hidden or system items. In order to see those, you will need to add the optional -Force switch.

You may find yourself needing to list either just the files or just the folders in your path, in which case you would use the following for our example path:

Get-ChildItem -Path "C:\Temp" -File (Lists files
Get-ChildItem -Path "C:\Temp" -Directory (Lists folders

If you find yourself needing to filter files by a certain file type, there are parameters for that! This cmdlet has two built-in parameters for filtering by file type. -Include and -Exclude can be used not only to filter by file type, but also allow for a wildcard (*) character to filter by parts of the file name. There are a few caveats to using these parameters in this way.

First, you must have a trailing '\*' in your path parameter, and this only works for files and not folders. That's not to say PowerShell doesn't have multiple different ways to accomplish filtering on folders, but it is not able to be done using this simpler way.

Get-ChildItem -Path "C:\Temp\*" -Include "*.txt" (
Get-ChildItem -Path "C:\Temp\*" -Exclude "*.pdf" (

2. Copying Files & Folders

Copy-Item is obvious in its name about what it does. That's one of the benefits of PowerShell and its verb-noun naming convention. Even for something as simple as copying items, there are a good handful of parameters that come with this cmdlet. The focus will be on the basic ones, which are very similar to some of the parameters for Get-ChildItem. The absolute minimum will be answering the questions "What do you want to copy?" and "Where do you want it copied to?" Say you have a file called LogFile.txt in your example directory, C:\Temp and you want to copy the file to a permanent home of C:\App\Logs:

Copy-Item -Path "C:\Temp\LogFile.txt" -Destination "C:\App\Logs\"

If for any reason, you want to change the name of the file you are copying on the fly, you can simply force the new name by placing the new name in the value of the -Destination parameter:

Copy-Item -Path "C:\Temp\LogFile.txt" -Destination "C:\App\Logs\

If you need to copy all the file contents of one folder to another, this can be done as well. Just be sure to use the wildcard (*) to signify all the files in the source path. This cmdlet will copy all the contents on C:\Temp to C:\App\Temp:

Copy-Item -Path "C:\Temp\*" -Destination "C:\App\Temp\" -Recurse

Don't forget the -Recurse swith, or all the folders that get copied from the root of the source path will just be empty folders in the destination without any of the files or folders within them.

The -Include and -Exclude parameters work the same way for this cmdlet as they did for Get-ChildItem. You can copy just certain file types or exclude specific file types. You can also use the wildcard (*) to easily filter parts of file names. To copy only .txt files you would input something like:

Copy-Item -Path "C:\Temp\*" -Destination "C:\App\Temp\" -Include "*.txt"

There are also lots of more targeted, in-depth filters that can be used for this cmdlet with the -Filter parameter. There is also myriad of combinations of this cmdlet to copy files and folders to and from remote PCs and servers on your network or domain. Check out the Microsoft Docs page for more examples and scenarios.

3. Creating Files & Folders

New-Item can certainly do the task of creating new files and folders, to which will be addressed here. It is suggested to see all the other items that it can create as well, not just simply files and folders. Check out the Microsoft Docs entry for an extensive listing. When it comes to creating files and folders using this cmdlet, it comes down to answering three questions: Where at? What type? What name?

The cmdlets will look very similar when it comes to create files or folders. The only difference will be the -ItemType parameter and the file extension if you're creating new files. Here are two examples for creating a folder named Reports in C:\App and then a creating a new .csv file within that new folder called Expenses:

New-Item -Path C:\App -Name "Reports" -ItemType "Directory"
New-Item -Path C:\App\Reports -Name "Expenses.csv" -ItemType "File"

A few things would be good to know with using this cmdlet. First, if creating a new file, be sure to include the desired file extension in the -Name parameter. If you leave off the file extension, a file will be created, but it will be unusable because the PC will not know what to do with it because it doesn't know what kind of file it is. Also, when using the -Force switch to create a folder, and the folder already exists, it won’t overwrite or replace the folder. It will simply return the existing folder object as an output to the console. However, if you use New-Item -Force on a file that already exists, the file will be completely overwritten.

4. Removing Files & Folders

Remove-Item is exactly what you think it is. We will focus on mainly removing files and folders, but as with all the others take some time to check out the Microsoft Docs entry and see an extensive list of the items this cmdlet can be used for. It can be used far beyond just the removal of files and folders. In terms of parameters, it has a very similar list of parameters as the other cmdlets that are discussed here. We will focus just on the simple ones to get started with becoming familiar with this cmdlet.

Given the nature of 'removing' something, please be sure to test extensively in an environment where you can stand to lose files and folders. This is not one to use in a production environment before you have tested the expected outcome thoroughly. Okay, a warning has been given. Now, on to removing all the things!

The simplest usage of this cmdlet only requires the -Path parameter. It answers the question, "What do you want to delete?" To delete your newly created Expenses.csv file that you created earlier:

Remove-Item -Path "C:\App\Reports\Expenses.csv"

Just a reminder, that there is no 'PowerShell Recycle Bin' of sorts for the items that get removed with this command, and the items removed do not drop into the GUI Recycle Bin, either.

To delete the previously created C:\App\Reports folder:

Remove-Item -Path "C:\App\Reports"

Notice that the folder gets removed as expected, but that is because the folder is empty. When using this cmdlet, if you attempt to remove a folder that has other folders and/or files inside of it, without using the -Recurse switch, you will be prompted for confirmation of the removal of all the objects within the folder. To remove hidden or read-only files, be sure to include the -Force switch, as without it you will be unable to remove any files flagged with these attributes.

There are also many ways to filter what to remove from a folder. The wildcard (*) will be your best ally in doing this. For example, if you want to target and remove any .csv reports from before the year 2021 and your file naming convention prepends all the report .csv files with the current year you would use the -Include  and -Exclude parameters together like so:

Remove-Item -Path C:\App\Reports\* -Include "*.csv" -Exclude "2021*"

Final Thoughts

Getting more familiar with these foundational cmdlets will certainly improve your PowerShell skills as you become more familiar with how they operate. If you are in any IT support role, think about how you could leverage these to make a specific task a little easier. If you talk with seasoned PowerShell vets, you will likely hear them talk about times in which they resorted to PowerShell to simply make something happen quicker and wanted the process to be repeatable. It will take a time investment on the front-end, but the returns are almost always positive.


Download

By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.


Don't miss out!Get great content
delivered to your inbox.

By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.

Recommended Articles

Get CBT Nuggets IT training news and resources

I have read and understood the privacy policy and am able to consent to it.

© 2024 CBT Nuggets. All rights reserved.Terms | Privacy Policy | Accessibility | Sitemap | 2850 Crescent Avenue, Eugene, OR 97408 | 541-284-5522