Change paths in Registry

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Seref Bey Seref Bey 2 years, 3 months ago.

  • Author
    Posts
  • #18184
    Profile photo of Seref Bey
    Seref Bey
    Participant

    Hi Everyone,

    I'm really new to PowerShell. My company is changing the paths for our Intranet site on some departments. Because of that I need to change some of the paths in the registry starting with OneNotebooks. I managed to write a script but it changes all the intranet sites. I only need to change it for certain departments like the ones below and I couldn't figure it out. Thank you eveyone in advance for their help.

    http://oldpath.test.intra/nemv
    http://oldpath.test.intra/finance
    http://oldpath.test.intra/sites/procurement/
    http://oldpath.test.intra/Comm
    http://oldpath.test.intra/Departments/SKS/
    http://oldpath.test.intra/ro/
    http://oldpath.test.intra/nitd
    http://oldpath.test.intra/cniedr
    http://oldpath.test.intra/communities/sig

    ########################
    #Set Variables
    $UserName = [Environment]::UserName
    $OneNoteBooks = "C:\backup\onenotenotebooks.txt"
    $NewPath = '//Newpath.test.net/'
    $Folder = "C:\backup"

    # Create a new folder if not exist file
    IF(-Not(Test-Path $Folder)){
    "Creating Backup Folde..!"
    New-Item -Path $Folder -ItemType Directory | Out-Default
    }
    "Done..!"

    # Start Logging
    Start-Transcript -Path "C:\backup\onenote.log"

    #Copy the OneNote backup folder
    "Copying OneNote Backup Folder" | Out-Default
    Copy-Item "C:\Users\$UserName\Local Settings\14.0\Backup\" "C:\backup" -Recurse -Force | Out-Default
    "Done..!"

    # Check if there is an existing file
    IF(-Not(Test-Path $OneNoteBooks))
    {
    "Creating New Text File to Export Registry Keys For backup" | Out-Default
    New-Item -Path $OneNoteBooks -ItemType File | Out-Default
    }
    #If exists change the file name with the date for backup purposes
    ELSE{
    "Found Existing Text File. Changing The Name With The Current Date..!"
    $Date = Get-Date -Format dd.MM.y.H.mm.s
    Move-Item -Path "C:\backup\onenotenotebooks.txt" $Folder\$Date.txt -Force | Out-Default
    New-Item $OneNoteBooks -ItemType File | Out-Default
    }

    # Exporting OneNote SharePoint Notebooks and Correcting them to the new URL
    "Exporting OneNote SharePoint Notebooks and Correcting them to the new URL..!" | Out-Default

    Push-Location
    Set-Location 'HKCU:\Software\Microsoft\Office\14.0\Onenote\opennotebooks'

    $OneNoteReg = Get-Item .
    $OneNoteReg.Property | ForEach-Object {
    $CurrentValue = $OneNoteReg.GetValue($_)
    $NewValue = $CurrentValue.ToString() | ForEach-Object { $_ -replace '//oldpath.test.intra/', $NewPath }
    Set-ItemProperty 'HKCU:\Software\Microsoft\Office\14.0\Onenote\opennotebooks' -Name $_ -Value $NewValue
    [PSCustomObject][Ordered]@{
    "Value Name"=$_
    "Original Value"=$CurrentValue
    "Updated Value"=$NewValue
    }
    } | Format-Table -AutoSize | Out-File $OneNoteBooks -Width 500

    Pop-Location
    "Done..!" | Out-Default
    "Migration finished. Please check the log file for any errors..!" | Out-Default

    #Stop Logging
    Stop-Transcript

  • #18192
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You need to parse the department out of the string and define an array of departments you do want processed. There is probably some nasty regex to strip out the department, but this is just some basic parsing:

    $deptToProcess = "nemv","finance","SKS","ro","nitd","cniedr","sig"
    
    $sites = "http://oldpath.test.intra/nemv",
             "http://oldpath.test.intra/finance",
             "http://oldpath.test.intra/sites/procurement/",
             "http://oldpath.test.intra/Comm",
             "http://oldpath.test.intra/Departments/SKS/",
             "http://oldpath.test.intra/IT/",
             "http://oldpath.test.intra/ro/",
             "http://oldpath.test.intra/nitd",
             "http://oldpath.test.intra/HR",
             "http://oldpath.test.intra/cniedr",
             "http://oldpath.test.intra/communities/sig"
    
    $sites | foreach {
        $site = $_
        
        #Strip off the trailing / if it exists, shouldn't be required in final path either
        if ($site.Substring($site.Length -1, 1) -eq "/") {
            $site = $site.Substring(0,$site.Length-1)
        }
        #Split the path into an array
        $arrPath = $site.Split("/")
        #Get the last item in the array
        $dept = $arrPath[$arrPath.Length -1]
        #Check against the defined $deptToProcess array if the current path should
        #be process
        if ($deptToProcess -contains $dept) {
            "Updating path to new site for: {0}" -f $site
            #Update code here
        }
        else {
            "Skipping path update for : {0}" -f $site
        }
    }
    

    Results:

    Updating path to new site for: http://oldpath.test.intra/nemv
    Updating path to new site for: http://oldpath.test.intra/finance
    Skipping path update for : http://oldpath.test.intra/sites/procurement
    Skipping path update for : http://oldpath.test.intra/Comm
    Updating path to new site for: http://oldpath.test.intra/Departments/SKS
    Skipping path update for : http://oldpath.test.intra/IT
    Updating path to new site for: http://oldpath.test.intra/ro
    Updating path to new site for: http://oldpath.test.intra/nitd
    Skipping path update for : http://oldpath.test.intra/HR
    Updating path to new site for: http://oldpath.test.intra/cniedr
    Updating path to new site for: http://oldpath.test.intra/communities/sig
    
  • #18200
    Profile photo of Seref Bey
    Seref Bey
    Participant

    Hmm.
    Thank you for your reply.
    This is way more complicated than I thought. How can implement this to my current script so that it actually changes the registry?

    Thanks,

  • #18202
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    It looks like you got your code from here: http://stackoverflow.com/questions/25047125/read-multiple-lines-from-text-file-and-added-it-to-registry

    There is a posted solution that looks like it's doing what you want, so is there a reason or something specific that is not working?

  • #18237
    Profile photo of Seref Bey
    Seref Bey
    Participant

    Hi Rob,

    Actually I posted that to that site as well. The solution was provided by "MadTechnichian". The solutuion was provided yesterday.

    Thanks for your help as well.

    Best.

You must be logged in to reply to this topic.