powershellreplace

replace is either working correctly, partial replacing, or not replacing at all. How do I get a consistent result


I have tried using .replace and -replace in my code but I cannot get a consistent result with it. I'm trying to remove "\server\folder\4. Software Installs\2. Network Printers\00000 installRicoh" from the path of a bunch of objects in the array $oldprintersripts Here is the code:

$oldprintersripts = @('\\server\folder\4. Software Installs\2. Network Printers\PleasantVille\WIB\YouthCorp','\\server\folder\4. Software Installs\2. Network Printers\00000 installRicoh\PleasantVille\WIB\HP401dn','\\cobsvit2\Infotech\4. Software Installs\2. Network Printers\00000 installRicoh\Northfield X\Stillwater\3rd Flr IDRC','\\cobsvit2\Infotech\4. Software Installs\2. Network Printers\00000 installRicoh\00000 installRicoh\Drexel\Support Work Front Office')
$printerfilepath = ""
Foreach($x in $oldprintersripts){
$x.Path
$printerfilepath = $x.path.Replace("\\cobsvit2\Infotech\4. Software Installs\2. Network Printers\00000 installRicoh\","")#[regex]::Escape('\\cobsvit2\Infotech\9. Scripts\New printer install Script\Printers\'))
$printerfilepath
"`n"

Here is the output I'm getting:

\cobsvit2\Infotech\4. Software Installs\2. Network Printers\PleasantVille\WIB\YouthCorp \cobsvit2\Infotech\4. Software Installs\2. Network Printers\PleasantVille\WIB\YouthCorp

\server\folder\4. Software Installs\2. Network Printers\00000 installRicoh\PleasantVille\WIB\HP401dn PleasantVille\WIB\HP401dn

\cobsvit2\Infotech\4. Software Installs\2. Network Printers\00000 installRicoh\Northfield X\Stillwater\3rd Flr IDRC Northfield X\Stillwater\3rd Flr IDRC

\cobsvit2\Infotech\4. Software Installs\2. Network Printers\00000 installRicoh\00000 installRicoh\Drexel\Support Work Front Office 00000 installRicoh\Drexel\Support Work Front Office

Both .replace("\server\folder\4. Software Installs\2. Network Printers\00000 installRicoh","") and -replace([regex]::Escape('\server\folder\9. Scripts\New printer install Script\Printers')) outputs are all over the place. Some outputs are correct, some are partially removed, and others nothing is happening. How do I get a consistent result.


Solution

  • I wasn't able to get the same output with the code you provided. It resulted in errors at "$x.Path"

    Perhaps this might work for you. From what I could tell this may be the output you're looking for.

    $oldprintersripts = @(
        '\\cobsvit2\Infotech\4. Software Installs\2. Network Printers\PleasantVille\WIB\YouthCorp',
        '\\cobsvit2\Infotech\4. Software Installs\2. Network Printers\00000 installRicoh\PleasantVille\WIB\HP401dn',
        '\\cobsvit2\Infotech\4. Software Installs\2. Network Printers\00000 installRicoh\Northfield X\Stillwater\3rd Flr IDRC',
        '\\cobsvit2\Infotech\4. Software Installs\2. Network Printers\00000 installRicoh\00000 installRicoh\Drexel\Support Work Front Office'
        )
    $printerfilepath = ""
    Foreach($x in $oldprintersripts){
        #Replaces from beginning of path to "Network Printers\", then replaces new beginning to "installRicoh\", then replaces beginning with your new path" 
        $printerfilepath = $x -replace "^.*Network\sPrinters\\" -replace "^.*installRicoh\\" -replace "^","\\cobsvit2\Infotech\9. Scripts\New printer install Script\Printers\"
        $printerfilepath
    "`n"
    }
    

    Output ($printerfilepath):

    \\cobsvit2\Infotech\9. Scripts\New printer install Script\Printers\PleasantVille\WIB\YouthCorp
    
    
    \\cobsvit2\Infotech\9. Scripts\New printer install Script\Printers\PleasantVille\WIB\HP401dn
    
    
    \\cobsvit2\Infotech\9. Scripts\New printer install Script\Printers\Northfield X\Stillwater\3rd Flr IDRC
    
    
    \\cobsvit2\Infotech\9. Scripts\New printer install Script\Printers\Drexel\Support Work Front Office
    

    If the above regex fails on additional filepaths not shown in your sample set, you could try Regular Expressions 101 site for testing/debugging.