Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Set-Content not updating file after get-content and forEach-Object

Reply
 
Old 06-14-2007   #1 (permalink)
Tolli


 
 

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 SpecsSystem Spec
Old 06-14-2007   #2 (permalink)
Kiron


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46