4

Using a Regex tester:
(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?>userDataXP>[a-zA-Z 0-9]*\$\$)) matches \\server\e$\users\user1$$

Using PowerShell I have to add a backtick before the second $ in the users filepath for it to match:

"\\server\e$\users\user1$`$" -match "(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))" True

Otherwise it fails:

"\\server\e$\users\user1$$" -match "(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))" False

The input file of the following script contains file paths with usernames all containing a $$. I want the regex to be able to capture the user$$, create a directory still with a $$, and copy files to it. How can I get around this problem in the following script?

$paths = get-content "E:\paths.txt" 
$ArchiveLocation = "\\Server1\Archive" 
$LogsData = Test-Path "$ArchiveLocation\Archived_UserData\Logs"
$LogsProfiles = Test-Path "$ArchiveLocation\Archived_UserProfiles\Logs"

$regxUsrProfPthXP = "(?<UserProfilePathXP>^((?![a-zA-Z 0-9]*\$\$).)*\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userProfileXP>[a-zA-Z 0-9]*\$))"
$regxUsrDataPthXP = "(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))"
if (-not $LogsProfiles) {
    md "$ArchiveLocation\Archived_UserProfiles\Logs"
}

if (-not $LogsData) {
    md "$ArchiveLocation\Archived_UserData\Logs"
}

foreach ($path in $paths) {

    if ($path -match $regxUsrProfPthXP) {
        $path = $matches.UserProfilePathXP
        $user = $matches.userProfileXP
        RoboCopy $path "$ArchiveLocation\Archived_UserProfiles\$user" /MIR /B /COPY:DATSOU /MOVE /LOG+:$ArchiveLocation\Archived_UserProfiles\Logs\$user.log /R:1 /W:1
    }

    if ($path -match $regxUsrDataPthXP) {
        $path = $matches.UserDataPathXP
        $user = $matches.userDataXP
        RoboCopy $path "$ArchiveLocation\Archived_UserData\$user" /MIR /B /COPY:DATSOU /MOVE /LOG+:$ArchiveLocation\Archived_UserData\Logs\$user.log /R:1 /W:1
    }


}
0

1 Answer 1

4

Just use single quotes where you define your regex and the string so you don't need to escape the $ using backticks.

'\\server\e$\users\user1$$' -match '(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))'

Output:

True

So all you have to do is to change

$regxUsrProfPthXP = "(?<UserProfilePathXP>^((?![a-zA-Z 0-9]*\$\$).)*\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userProfileXP>[a-zA-Z 0-9]*\$))"
$regxUsrDataPthXP = "(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))"

to

$regxUsrProfPthXP = '(?<UserProfilePathXP>^((?![a-zA-Z 0-9]*\$\$).)*\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userProfileXP>[a-zA-Z 0-9]*\$))'
$regxUsrDataPthXP = '(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))'
Sign up to request clarification or add additional context in comments.

3 Comments

I can use single quotes for the regex but how can I do that for the strings which are taken by looping an input file that does not include quotes?
you don't need to do it there. If the $path within your foreach loop contains \\server\e$\users\user1$$ and you defined your regex using single quotes, it will work
As simple as that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.