# Change paths in Registry

**URL:** <https://forums.powershell.org/t/change-paths-in-registry/2995>\
**Category:** PowerShell Help\
**Created:** [August 19, 2014, 11:42pm UTC](https://forums.powershell.org/t/change-paths-in-registry/2995 "2014-08-19T23:42:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr-seref-bey](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@mr-seref-bey](https://forums.powershell.org/u/mr-seref-bey)\
**Post date:** [August 19, 2014, 11:42pm UTC](https://forums.powershell.org/t/change-paths-in-registry/2995/1 "2014-08-19T23:42:27Z")

</div>

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/nemv)  
[http://oldpath.test.intra/finance](http://oldpath.test.intra/finance)  
[http://oldpath.test.intra/sites/procurement/](http://oldpath.test.intra/sites/procurement/)  
[http://oldpath.test.intra/Comm](http://oldpath.test.intra/Comm)  
[http://oldpath.test.intra/Departments/SKS/](http://oldpath.test.intra/Departments/SKS/)  
[http://oldpath.test.intra/ro/](http://oldpath.test.intra/ro/)  
[http://oldpath.test.intra/nitd](http://oldpath.test.intra/nitd)  
[http://oldpath.test.intra/cniedr](http://oldpath.test.intra/cniedr)  
[http://oldpath.test.intra/communities/sig](http://oldpath.test.intra/communities/sig)

########################  
#Set Variables  
$UserName = [Environment]::UserName  
$OneNoteBooks = “C:\backup\onenotenotebooks.txt”  
$NewPath = ‘[//Newpath.test.net/](https://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/](https://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

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [August 20, 2014, 2:11am UTC](https://forums.powershell.org/t/change-paths-in-registry/2995/2 "2014-08-20T02:11:31Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![mr-seref-bey](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@mr-seref-bey](https://forums.powershell.org/u/mr-seref-bey)\
**Post date:** [August 20, 2014, 4:03am UTC](https://forums.powershell.org/t/change-paths-in-registry/2995/3 "2014-08-20T04:03:59Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [August 20, 2014, 7:19am UTC](https://forums.powershell.org/t/change-paths-in-registry/2995/4 "2014-08-20T07:19:23Z")

</div>

It looks like you got your code from here: [powershell - read multiple lines from text file and added it to registry - Stack Overflow](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?

---

<div class="post-metadata">

**Author:** ![mr-seref-bey](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@mr-seref-bey](https://forums.powershell.org/u/mr-seref-bey)\
**Post date:** [August 21, 2014, 12:20am UTC](https://forums.powershell.org/t/change-paths-in-registry/2995/5 "2014-08-21T00:20:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:47pm UTC](https://forums.powershell.org/t/change-paths-in-registry/2995/6 "2024-05-16T20:47:39Z")

</div>

Closed on May 16, 2024, 8:47pm UTC


