kb2487 kb2487 - 1 year ago 92
PowerShell Question

Update part of text in document

I have a

document which can be opened with notepad just fine. Below is the part of it that is important to me:








I would like to create a script, which would auto replace part of text
in line 128 (it is some old date - 26.05.2014) and replace it with the current date.

For example: today it should be
, tommorrow there should be
, etc.

As said before text is in line 128, characters are from 24 to 30.

If searched around (SED for WIN, VBA, Powershell,...) but i didn't find anything that could work.

Thank you all in advance!

Answer Source

Here a solution using PowerShell:

$content = Get-Content 'your_file_path'
$content[127] = $content[127] -replace '(.{24}).{6}', ('${{1}}{0}' -f (Get-Date).ToString('yyMMdd'))
$content | Set-Content 'your_file_path'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download