![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Set-Content not updating file after get-content and forEach-Object I have a file I need to append a value to the end of a specific line of text (in a random location in the file). First entry works (replaces text in file) second one does not update file at the end of the line. I can't use the first entry because I can't do a -replace. 1# (get-content $file) | foreach-object {$_ -replace "Group Membership","My Membership"} | Set-Content $file 2# (Get-Content $File) | ForEach-Object {if ($_.contains("S-1-5-32-545__Members") -eq $true) {$_ = $_+",*S-1-5-4" }} | Set-Content $File Even though $_ is being updated with the new value, that new value is not being passed on to the Set-Content command. |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Set-Content not updating file after get-content and forEach-Object The if block changes the value of the object but it doesn't send it through the pipeline; add another statement inside the block that will send the changed object through. Also, need to add an else statement to output the lines that don't match the if condition: (Get-Content $File) | ForEach-Object {if ($_.contains("S-1-5-32-545__Members") -eq $true) {$_ = $_+",*S-1-5-4"; $_} else {$_}} | Set-Content $File You can also use -replace: (Get-Content $File) | ForEach-Object {$_ -replace "S-1-5-32-545__Members", "S-1-5-32-545__Members,*S-1-5-4"} | Set-Content $File -- Kiron |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Get-content -wait not updating? | PowerShell | |||
| Get-Content and Group-Object | PowerShell | |||
| Issue: getting/setting variable content using Get/Set-Content | PowerShell | |||
| About adding content to the same file using "add-content" | PowerShell | |||
| Weirdness with get-content | replace | set-content - file content is deleted!! | PowerShell | |||