\n Hi, and welcome to the PowerShell forum! \n\n\nDon’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!\nUse a descriptive subject. Don't say \"Need help\" or \"PowerShell Help\", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…\n <\/blockquote>\n<\/aside>\n\n
<\/p>","upvoteCount":2,"datePublished":"2019-02-20T18:40:14.000Z","url":"https://community.spiceworks.com/t/powershell-edit-and-then-save-existing-file/698580/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
What have you tried? We can help you with a script, but we do not write scripts from scratch on demand.<\/p>","upvoteCount":1,"datePublished":"2019-02-20T18:41:00.000Z","url":"https://community.spiceworks.com/t/powershell-edit-and-then-save-existing-file/698580/3","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"
I’m very new to PS, so I’ve gotten quite intimate with Google and Spiceworks on this issue over the last couple of days … this is what I have so far;<\/p>\n
# Rename the file\ncd \"E:\\path\\\"\nren \"logfile.txt\" $(\"logfile_\"+$(Get-Date -format yyyyMMdd_HHmmss)+\".txt\")\n\n# Remove all lines over 25,000 from the top\n\n# Delete all files older than x days\n$Path = \"E:\\path\\\"\n$Daysback = \"-x\"\n \n$CurrentDate = Get-Date\n$DatetoDelete = $CurrentDate.AddDays($Daysback)\nGet-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item\n<\/code><\/pre>\nI’m not sure where to start on the issue of editing the file.<\/p>","upvoteCount":0,"datePublished":"2019-02-20T18:47:01.000Z","url":"https://community.spiceworks.com/t/powershell-edit-and-then-save-existing-file/698580/4","author":{"@type":"Person","name":"coreypoulton","url":"https://community.spiceworks.com/u/coreypoulton"}},{"@type":"Answer","text":"
well,<\/p>\n
$file1=\"\\\\server\\share\\file.txt\"\n\n(Select-String -Pattern \"\\w\" -Path $file1 | foreach {$_.line} | Select-Object -Skip 25000 | Out-File -FilePath $file1)\t\n<\/code><\/pre>\nyou could try that.<\/p>","upvoteCount":1,"datePublished":"2019-02-20T18:48:14.000Z","url":"https://community.spiceworks.com/t/powershell-edit-and-then-save-existing-file/698580/5","author":{"@type":"Person","name":"anthony7445","url":"https://community.spiceworks.com/u/anthony7445"}},{"@type":"Answer","text":"
Pick file, select first 25000 lines, save it.<\/p>\n
# an idea\nget-content $LogFile |\nselect-object -first 25000 |\nout-file $logFileNew\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2019-02-20T18:48:37.000Z","url":"https://community.spiceworks.com/t/powershell-edit-and-then-save-existing-file/698580/6","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"I do this in the script that I run every morning, M-F with the exception of saving the old data.<\/p>\n
You could easily add a line that simply copied the file as is and saved it as a new file before doing the following.<\/p>\n
Set your max lines as what you want left in the file.<\/p>\n
$maxLines = 200\n$logFile = \"C:\\MyFolder\\output.log\"\n\n# Trim the max size of the log file\n(Get-Content $logFile -tail $maxLines -ReadCount 0) | Set-Content $logFile\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2019-02-20T18:52:13.000Z","url":"https://community.spiceworks.com/t/powershell-edit-and-then-save-existing-file/698580/7","author":{"@type":"Person","name":"kevinboutelle","url":"https://community.spiceworks.com/u/kevinboutelle"}},{"@type":"Answer","text":"Stop mixing batch and PowerShell and your life will become a lot easier. Made that mistake too in the beginning and had some freaky unexplainable results due to invisible characters. It’s hard in the beginning but try and dont be afraid to post your code for critique. Learn from the critique.<\/p>","upvoteCount":2,"datePublished":"2019-02-20T18:59:34.000Z","url":"https://community.spiceworks.com/t/powershell-edit-and-then-save-existing-file/698580/9","author":{"@type":"Person","name":"edwineekelaers2","url":"https://community.spiceworks.com/u/edwineekelaers2"}},{"@type":"Answer","text":"
Yeah, I inherited the file from another person that had originally made the renaming part of the script. I added the code to delete after x amount of days and those two seem to be testing okay. So, I thought I’d just rewrite the original renaming part later. I’m always willing to accept some constructive feedback and critiquing … so feel free to fire away
<\/p>","upvoteCount":2,"datePublished":"2019-02-20T19:24:19.000Z","url":"https://community.spiceworks.com/t/powershell-edit-and-then-save-existing-file/698580/10","author":{"@type":"Person","name":"coreypoulton","url":"https://community.spiceworks.com/u/coreypoulton"}},{"@type":"Answer","text":"