Home > Software design >  Change a value of an .ini files in multiple directories
Change a value of an .ini files in multiple directories

Time:10-14

I need to change a value in the DefaultPrint.ini file in multiple users "home" directory.

I've managed to make a powershell script to change the value on a single user, but I am struggeling making the script accept multiple users.

$User = "Max"

$IniPath = "\\Network1\Home\$User\"

foreach ($User in $User)
    {   
        $IniFile = "$IniPath\DefaultPrint.ini"
     
        $IniText = Get-Content -Path $IniFile

        $NewText = $IniText.Replace("Old_Priter","New_Printer")

        Set-Content -Path $IniFile -Value $NewText -Force
    }

The directories I need to enter to find the files is based on the usernames, so I need to change the file in: \Network1\Home\Max
\Network1\Home\John
\Network1\Home\Sophia
etc

The script above is working for a single user, and I am trying to adapt it to work with multiple users though a .csv file

I tried the following

$User = "Max","John","Sophia"

But it does not seperate the user directories but gathers them togther? (\Network1\Home\Max John Sophia)

I also tried to import via csv, as I need to do this on 200 users

$User = (Import-Csv -Path .\Users.csv).User

But it ends up doing the same, what am I doing wrong?

CodePudding user response:

You need to move this statement:

$IniPath = "\\Network1\Home\$User\"

inside the loop, so that the path is updated with the correct $user every time, and then rename the $User variable used outside the loop ($Users seems an appropriate name here):

$Users = -split "Max John Sophia"

foreach ($User in $Users)
{   
    $IniPath = "\\Network1\Home\$User\"

    $IniFile = "$IniPath\DefaultPrint.ini"
 
    $IniText = Get-Content -Path $IniFile

    $NewText = $IniText.Replace("Old_Priter","New_Printer")

    Set-Content -Path $IniFile -Value $NewText -Force
}
  • Related