Replace or delete certain characters from filenames of all files in a folder

Dir | Rename-Item -NewName { $_.Name -replace " ","_" }

EDIT :
Optionally, the Where-Object command can be used to filter out ineligible objects for the successive cmdlet (command-let). The following are some examples to illustrate the flexibility it can afford you:

  • To skip any document files

    Dir | Where-Object { $_.Name -notmatch "\.(doc|xls|ppt)x?$" } | Rename-Item -NewName { $_.Name -replace " ","_" }

  • To process only directories (pre-3.0 version)

    Dir | Where-Object { $_.Mode -match "^d" } | Rename-Item -NewName { $_.Name -replace " ","_" }

    PowerShell v3.0 introduced new Dir flags. You can also use Dir -Directory there.

  • To skip any files already containing an underscore (or some other character)

    Dir | Where-Object { -not $_.Name.Contains("_") } | Rename-Item -NewName { $_.Name -replace " ","_" }

 

 

 

PowerShell: How do I append a prefix to all files in a directory?

Get-ChildItem | Rename-Item -NewName { "Prefix_" + $_.Name }

 

How to Use PowerShell and 7zip to create individual zip file for each .dmp file in a directory

 

  1. Start up PowerShell from the Start menu

    1. Make sure to 'run as administrator'

  2. Move to the folder where your .dmp files are located

  3. Use the following command line script to kick off the processing of the .dmp files

    dir *.dmp | ForEach-Object { & "C:\Program Files\7-Zip\7z.exe" a -tzip ($_.Name+".zip") $_.Name }
  4. Here is a break down of the commands

    1. dir *.dmp

      1. selects all files in the directory

      2. use a different extension for different file types

    2. |

      1. passes the results of the first command to the next
    3. ForEach-Object
      1. loops thru all the elements from the first command
    4. "C:\Program Files\7-Zip\7z.exe"
      1. The path on my PC for the 7z.exe command line executable
    5. a
      1. The 7zip command to add to an archive
    6. -tzip
      1. the 7zip command to create a zip archive
    7. ($_.Name+".zip")
      1. the 7zip command to name the archive created
        1. $_.Name
          1. the PowerShell command to get the full name of the file 
        2. +".zip"
          1. adds the .zip extension to the archive created
    8. $_.Name
      1. the 7zip command for the file to archive
      2. the PowerShell command to get the full name of the file