Removing the spaces in your file name using PowerShell

I was recently working on some import-csv commands and scripts that required data from multiple sources.  As I have no control over how other people name their files (some uses spaces) I needed to ensure that the file names in certain folders have no spaces.  Its nicer to have files with no spaces in the name.  I’m old fashioned.

So below is a little command I can add before the core of my scripts kick off

As you can see from above I have defined my $path – this is the full path to the folder I want to work on.

The Get-ChildItem command is used to get a list of all the items in my $path.

If I only want to change file names then I use the -File option.  If I wanted to remove the spaces on all items, including Directory names I would remove the -File option.  ** Renaming folder names can cause major headaches – be careful **

Another option is to add the -Recursive this will drill down and rename all files in all subfolders also and it would look like this.


Leave a Reply

Your email address will not be published. Required fields are marked *