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 7534623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:04:28+00:00 2026-05-30T06:04:28+00:00

I am in the process of converting thousands of lines of batch code into

  • 0

I am in the process of converting thousands of lines of batch code into PowerShell. I’m using regex to help with this process. The problem is part of the code is:

$`$2

When replaced it just shows $2 and doesn’t expand out the variable. I’ve also used single quotes for the second portion of replace instead of escaping the variables, same result.

$origString = @'
IF /I "%OPERATINGSYSTEM:~0,6%"=="WIN864" SET CACHE_OS=WIN864
...many more lines of batch code
'@

$replacedString = $origString -replace "(IF /I `"%)(.+)(:.+%`"==`")(.+`")(.+)","if ( $`$2 -match `"^`$4 ) {`$5 }"

$replacedString
  • 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-05-30T06:04:29+00:00Added an answer on May 30, 2026 at 6:04 am

    You could try something like this:

    $origString -replace "(IF /I `"%)(.+)(:.+%`"==`")(.+`")(.+)",'if ($$$2 -match "^$4" ) {$5 }'
    

    Note the $$$2. This evaluates to $ and content of $2.


    Some code to show you the differences. Try it yourself:

    'abc' -replace 'a(\w)', '$1'
    'abc' -replace 'a(\w)', "$1"  # "$1" is expanded before replace to ''
    'abc' -replace 'a(\w)', '$$$1'
    'abc' -replace 'a(\w)', "$$$1" #variable $$ and $1 is expanded before regex replace
                                   #$$ and $1 don't exist, so they are expanded to ''
    
    $$ = 'xyz'
    $1 = '123'
    'abc' -replace 'a(\w)', "$$$1`$1" #"$$$1" is expanded to 'xyz123', but `$1 is used in regex
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am in the process of converting a static website into one using a
I went through the process of converting mysql_* code into PDO code. I've run
Im in the process of converting my rails code which developed in version 2
I am in process of converting Visual Basic app into Python Django. Currently, it
I am in the process of converting views from ASP.NET MVC TagSoup to using
I'm in the process of converting our java code to use NIO, but I'm
Using C++ with Visual Studio 2010. I'm in the process of converting my NULL
We're in the process of converting web app to IE8 (now client is using
I'm in the process of converting my non secure socket to ssl, using boost::asio::ssl
I'm in the process of converting my Microsoft SDK Beta code to the Microsoft

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.