PowerShell Regular Expression: Find and Replace default username with current username in an .XML configuration file

I’ve below Config file in which there are multiple instances of user profile path (C:UsersCoreluser001.Testlab.*).  This user profile path points to a default user and needs to be updated to the currently logged on user profile path (C:Users<username>.*).

 

PS C:> grep -i "Coreluser001" C:TempCorelConnect-Default.config
<AppPrefs xmlns:xsd="
http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" TrayRootDirectory="C:UsersCoreluser001.TestlabDocumentsCorelCorel ContentTrays" SelectedTray="C:UsersCoreluser001.TestlabDocumentsCorelCorel ContentTraysTray">
    <Folder Provider="6253a437-710e-4bda-9e51-052b9f3f41c1" LanguageIndex="0" DisplayContentIndex="0" NumResultsIndex="0" IsSearched="true" IsActivated="true" StoredTitle="Documents" Path="C:UsersCoreluser001.TestlabDocuments" />
    <Folder Provider="6253a437-710e-4bda-9e51-052b9f3f41c1" LanguageIndex="0" DisplayContentIndex="0" NumResultsIndex="0" IsSearched="false" IsActivated="true" StoredTitle="My Pictures" Path="C:UsersCoreluser001.TestlabPictures" />
    <Folder Provider="6253a437-710e-4bda-9e51-052b9f3f41c1" LanguageIndex="0" DisplayContentIndex="0" NumResultsIndex="0" IsSearched="true" IsActivated="true" StoredTitle="Desktop" Path="C:UsersCoreluser001.TestlabDesktop" />
    <Searchable xsi:type="Folder" Provider="6253a437-710e-4bda-9e51-052b9f3f41c1" LanguageIndex="0" DisplayContentIndex="0" NumResultsIndex="0" IsSearched="true  IsActivated="true" StoredTitle="Desktop" Path="C:UsersCoreluser001.TestlabDesktop" />
    <Searchable xsi:type="Folder" Provider="6253a437-710e-4bda-9e51-052b9f3f41c1" LanguageIndex="0" DisplayContentIndex="0" NumResultsIndex="0" IsSearched="true" IsActivated="true" StoredTitle="Documents" Path="C:UsersCoreluser001.TestlabDocuments" />
PS C:>

 

Below is the PowerShell code that fetches the currently loggedon user name and replaces the default user profile path with current user profile path globally in the .config file.

 

$UserName = $Env:USERNAME
$InputFile = "C:TempCorelConnect-Default.config"
$FileContent = Get-Content -path $InputFile
$FileContent[1] -match "C:\users\(w+)\*.*"
$MatchStr = $Matches[1]

$FileContent | foreach {$_ -replace $MatchStr, $UserName } | Set-Content $InputFile

 

The contents of the file after the string replacement by script:

  • No default user (Coreluser001) path
  • is replaced by current user (Govardhan) path

PS C:> grep -i "Coreluser001" C:TempCorelConnect-Default.config
PS C:> grep -i "$env:username" C:TempCorelConnect-Default.config
<AppPrefs xmlns:xsd="
http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" TrayRootDirectory="C:UsersGovardhan.TestlabDocuentsCorelCorel ContentTrays" SelectedTray="C:UsersGovardhan.TestlabDocumentsCorelCorel ContentTraysTray">
    <Folder Provider="6253a437-710e-4bda-9e51-052b9f3f41c1" LanguageIndex="0" DisplayContentIndex="0" NumResultsIndex="0" IsSearched="true" IsActivated="true" toredTitle="Documents" Path="C:UsersGovardhan.TestlabDocuments" />
    <Folder Provider="6253a437-710e-4bda-9e51-052b9f3f41c1" LanguageIndex="0" DisplayContentIndex="0" NumResultsIndex="0" IsSearched="false" IsActivated="true" StoredTitle="My Pictures" Path="C:UsersGovardhan.TestlabPictures" />
    <Folder Provider="6253a437-710e-4bda-9e51-052b9f3f41c1" LanguageIndex="0" DisplayContentIndex="0" NumResultsIndex="0" IsSearched="true" IsActivated="true" toredTitle="Desktop" Path="C:UsersGovardhan.TestlabDesktop" />
    <Searchable xsi:type="Folder" Provider="6253a437-710e-4bda-9e51-052b9f3f41c1" LanguageIndex="0" DisplayContentIndex="0" NumResultsIndex="0" IsSearched="true" IsActivated="true" StoredTitle="Desktop" Path="C:UsersGovardhan.TestlabDesktop" />
    <Searchable xsi:type="Folder" Provider="6253a437-710e-4bda-9e51-052b9f3f41c1" LanguageIndex="0" DisplayContentIndex="0" NumResultsIndex="0" IsSearched="true" IsActivated="true" StoredTitle="Documents" Path="C:UsersGovardhan.TestlabDocuments" />
PS C:>

Leave a Reply

Your email address will not be published. Required fields are marked *