I am new to Windows PowerShell. I am trying to perform a find and replace string on 4 occasions. But even a simple find and replace is throwing an
Exception of type 'System.OutOfMemoryException' was thrown.
error. I used Get-content
.
Is there a way to achieve it without disrupting the memory?
e.g. Replace ".000000000Z" with ".732Z" where 732 will be the milliseconds the job is run?
PSversion: 3.0
The typical method is to use .Net methods to do it line by line.
Assuming you've got sensible line breaks, you can do something like this:
If your XML file is a single line of text, it gets more complicated.
Get-Content loads the entire file content into RAM to be acted upon.
You need to upgrade your RAM Use a deferent method and there are a few of them.
The .Net reader is the most optimal
System.IO.File.ReadLines() is more than likely your best choice as it returns all the lines of a file, but lets you begin iterating over the lines immediately which means it does not have to store the entire contents in memory. More details here: https://msdn.microsoft.com/en-us/library/dd383503.aspx
So, you could do something like this...
Or shorten it to this...