Trimming dollar sign from folder names

Welcome Forums General PowerShell Q&A Trimming dollar sign from folder names

This topic contains 4 replies, has 4 voices, and was last updated by

2 years, 6 months ago.

  • Author
  • #54835

    Points: 0
    Rank: Member

    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

    Points: 63
    Rank: Member

    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

    Points: 0
    Rank: Member

    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

    Points: 0
    Rank: Member

    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

    Points: 2
    Rank: Member

    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.

The topic ‘Trimming dollar sign from folder names’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort