Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9208853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:42:25+00:00 2026-06-18T00:42:25+00:00

I need to replace square brackets in filenames, and I’ve successfully created (and validated

  • 0

I need to replace square brackets in filenames, and I’ve successfully created (and validated at the console) a -replace. Now I’m trying to Move-Item to a new directory because this bug in Powershell 2.0 prevents me from doing a simple file rename.

Here’s my script:

$txtPath = "c:\users\xxxxxx\desktop\cgc\tx"     #source files
$txtPath2 = "c:\users\xxxxxx\desktop\cgc\tx2"   #renamed files
Get-ChildItem $txtPath | foreach { 
    Move-Item -literalpath C:\users\xxxxxx\desktop\test001 ($_.Name -replace '\{|}','_') 
}

Here’s what’s happening: I was using the $txtPath2 variable, but kept getting “cannot bind to null directory” errors, so I explicitly coded to the path to see if there was something odd with how the variable parsed. Now, I get this error:

Move-Item : Cannot move item because the item at 'C:\users\xxxxxx\desktop\test001' does not exist.
At C:\users\xxxxxx\desktop\cgc\rni.ps1:5 char:10
+ Move-Item <<<<  -literalpath C:\users\xxxxxx\desktop\test001 ($_.Name -replace '\{|}','_')
    + CategoryInfo          : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand

Here’s what’s odd: I create the new directory. I run the script, watch it vanish from my desktop as the script fails. WTF? I’ve quit and restarted the console application to flush any variables. I’ve tried different flavors of variable vs. constant in the Move-Item line. Unless there’s a Move-Item parameter that I’m missing, I really have no idea what’s going on. Does anyone else see anything that would cause my file to be deleted?

EDIT:
After editing to

Get-ChildItem $txtPath | % { [system.io.file]::Move($_.fullname, ($i.FullName -replace '\[|\]', '') ) }

I get a new error:

Exception calling "Move" with "2" argument(s): "Empty file name is not legal.
Parameter name: destFileName"
At C:\users\x46332\desktop\cgc\rni.ps1:6 char:52
+ Get-ChildItem $txtPath | % { [system.io.file]::Move <<<< ($_.fullname, ($i.FullName -replace '\[|\]', '') ) }
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:42:26+00:00Added an answer on June 18, 2026 at 12:42 am

    You set a modified version of $_.Name as destination (second arg). “Name” is just the FILEname of an item, so I’m guessing your test001 file/folder got moved to the place you run the script from and renamed to whatever $_.Name was (it uses Name as a relative path). So if you run this script from c:\windows\system32 (default folder when PS is running as admin), you move it there.

    The next time in your foreach-loop, test001 is already moved and it returns an error. -LiteralPath is source location, not destination.

    Try:

    Get-ChildItem $txtPath | % { [system.io.file]::Move($_.fullname, ($_.FullName -replace '\[|\]', '') ) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to replace every instance of any text in square brackets with something
I need to replace more than 20 000 names with new names i created
I need split string by comma, but comma shouldn`t be inside square brackets. For
I have a list of numbers between square brackets, and I need to add
I need replace slide effect to fade-out/fade-in effect on the prev and next slide
Need to replace a domain name on all the links on the page that
I need to replace some value in given text using c# preferably using regex
I need to replace a list of image src in a div tag. For
I need to replace the Bind event from JQuery with some event from JavaScript.
I need to replace a word AAAA in a file using dictionary: dictionary.txt AXF1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.