Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Need to modify text in a file.

$
0
0

So I have some entries in my Hosts file that are used for testing.  I need to swap two of the pieces of data and am having trouble.

I read in the data like this:

$hosttemp = Get-Content("\\machine\c`$\windows\system32\drivers\etc\hosts")

If I then just type $hosttemp it spits out my current host file which is good.

I then tried to do a $hosttemp.replace("old text","new text") but I get an error that it doesn't have a 'replace' method.

I then did $hosttemp | gm and it shows me it's a System.String and that it has a Replace method.

However, if I then do $hosttemp.getType() it says:

sPublic IsSerial Name                                     BaseType                                                        
-------- -------- ----                                     --------                                                        
True     True     Object[]                                 System.Array
What can I do so that I can read in the file, do a couple replaces, and then set-content the file back where it came from?  I don't know why GM shows me it's a string and has a Replace method but then it tells me it's an array and I can't use those methods...

Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>