Trimming dollar sign from folder names

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 2 weeks, 3 days ago.

  • Author
  • #54835
    Profile photo of Ken Resler
    Ken Resler

    I inherited a system where the previous admin made a dollar sign part of the folder name for the users home drive. He was thinking this would hide the folder from other users viewing it. He was confusing that with adding the $ to the share name. So the path looks something like \\servername\userid$

    These folders are all on two 2003 servers. We are combining them down to one location and using DFS on 2012R2 servers. We want to get rid of the dollar signs after moving the folders to the new server.

    I want to trim the $ sign off or rename the folder I have been trying to use a one line code that looks like this

    Get-ChildItem *_* | Rename-Item -NewName { $_.Name -replace '_',' ' }

    In my testing if the folder in question has the underscore and I want to replace it with a space it works no problem.

    But if I change my folder name to have the $ it fails. The code looks like below and the error is shown below.

    Get-ChildItem *$* | Rename-Item -NewName { $_.Name -replace '$',' ' }

    Error message:
    Rename-Item : Source and destination path must be different.
    At line:1 char:21
    + Get-ChildItem *$* | Rename-Item -NewName { $_.Name -replace '$',' ' }
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : WriteError: (E:\div\its\its\kenr\test_new\newdir3$:String) [Rena
    me-Item], IOException
    + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemComman

    Any thoughts, suggestions, code snipets…. Would be greatly appreciated.

  • #54836
    Profile photo of random commandline
    random commandline

    Remove -WhatIf to execute command ; -WhatIf is used to test what would happen.

    (Get-ChildItem \\path\to\folders -Filter *$*) |
    Rename-Item -NewName {$_.Name -replace '\$'} -WhatIf
  • #54843
    Profile photo of Ken Resler
    Ken Resler

    To whomever posted this thank you that worked. Two questions what does the \$ do to the code? I had someone suggest a back tick` in the same spot but that did not work. I suspected all along that this was a special character issue. Second question is why don't I have to specify what to replace the $ with?

    Again thank you.

  • #54848
    Profile photo of Jonathan Warnken
    Jonathan Warnken

    The back tick` is an escape character for powershell. The -replace command is a dot net method and uses the dot net escape character \

  • #54867
    Profile photo of Curtis Smith
    Curtis Smith

    Second question is why don't I have to specify what to replace the $ with?

    You are. You are specifying nothing. By not adding something to replace it with, you are telling it to replace it with nothing.

You must be logged in to reply to this topic.