I have a file that I am downloading daily what I want to do is create a script that reads the text file and deletes out any lines containing the string "---"
I started with "(get-content L:\holdings.txt) | where {$_ -ne "" | out-file holdings.txt"
This deletes all the blank lines without any issue, I then tried
$del -like "*-*"
"(get-content L:\holdings.txt) | where {$_ -ne $del | out-file L:\holdings.txt"
Which I though would delete out any of the lines of data, whilst it runs without error the line is not being deleted out...
I don't know if it's relevant but the line I want to delete will always be the second line as it is a comma delimited SQL batch file automation.
The file reads as follows
Advice Num ,Bargain Number
--------------------,-----------------
Just to try and give you a graphical representation that I am trying to resolve
Any ideas?
Regards
Richard
Solved it, a quick amendment and here it is (get-content "L:\movement.txt") -notmatch "---" | out-file "L:\movement.txt"
Related
We have a lot of users with duplicate files on their Desktop (Windows 10). I want to create a script which runs every X minutes or just once after logging on to the system.
Our users are currently seeing the following shortcuts (.lnk):
Dummy.lnk (correct shortcut, should remain unchanged)
Dummy - copy.lnk (duplicated shortcut, should be removed)
Dummy-OFFICE-QB4V70A.lnk (duplicated shortcut containing the name of the computer, should be removed)
So in short I want to keep the 'original' shortcuts but I want to remove all shortcuts containing '- copy.lnk' and/or '-computername.lnk'.
I also tried using the %computername% variable but I have no idea how to implement this in my code. Every computer has a different and unique name so I can not use pre-defined computernames.
Any tips on how I can achieve this? I have tried using the code below, but this removes all .lnk (shortcut) files and not just the duplicates.
#(Get-ChildItem Dummy.lnk | Select-String -Pattern "OFFICE" | Select-Object -ExpandProperty path -Unique) | ForEach-Object{Remove-Item -Force -LiteralPath $_}
I prefer using Powershell so that I can deploy the script using Group Policy.
I would use the Where-Object cmdlet to filter all shortcuts you want to remove and pipe them to the Remove-Item cmdlet:
Get-ChildItem -Path 'c:\yourPath' -Filter '*.lnk' |
Where-Object { $_.Name -match '- Copy\.lnk$' -or $_.Name -like "*$($env:Computername)*"} |
Remove-Item
I have found an answer that gives the list of files in powershell here
How to search a string in multiple files and return the names of files in Powershell?
but I need to know the line number and line text also where that text is found. After this I need to write the output to an excel or csv.
edit:
Get-ChildItem -recurse | Select-String -pattern "ftp" | Write-Host script is giving me the output as C:\codes\prog1.txt:10:FTP but I want the output as path - C:\codes\prog1.txt line number-10 Line text-FTP "server1" in Excel
Try this (don't know if you only want the filename or the path to the file, just remove the one you dont want):
Get-ChildItem -recurse | Select-String -pattern "string" | Select-Object path,line,linenumber,filename | Export-Csv -Path c:\somepath\result.csv
Update - tried text without any back ticks still not replacing.
I have a file test.txt with these records:
name="BLUE_TBL_AC_EA_FREQ"` owner="BLUE_TBL_AC_EA_FREQ"
name="BLUE_TBL_AC_EA_FREQ" owner="RED_TBL_AC_EA_FREQ"
I would like to replace the string for the name attribute so I would use
name="BLUE_TBL_AC_EA_FREQ"
In the powershell script. This is what I did but the output file does not contain the replaced text, it is the same as the input.
Then I use a batch script that has this powershell command in the batch script
powershell -Command "(Get-Content "test.txt") | Foreach-Object {$_ -replace 'name="BLUE_TBL_AC_EA_FREQ"', 'name="BLUE_TBL_ACEAFREQ"'} | Set-Content "testo.txt""
The batch script does not replace the string that contains the string.
Am I doing something wrong, do I have to escape the double quotes for the string?
It is still not working for me.
End update
I am trying to use Powershell to replace some text in a file that makes use of the back tick.
In the file there is text such as
' name="BLUE_TBL_AC_EA_`FREQ"'
I would like to replace this text by
' name="BLUE_TBL_ACEAFREQ"'
The replace that I use in Powershell look like this
powershell -command "& {(Get-Content "file.txt") | Foreach-Object {$_ -replace ' name="BLUE_TBL_AC_EA_`FREQ"', ' name="BLUE_TBL_ACEAFREQ"'} | Set-Content "fileO.txt;}"
The ' name= is needed since there are multiple strings that contain the name="... string.
When I use this command nothing gets replaced. I have tried to escape the back tick by using a double back tick (``) and still nothing gets replaced.
I have searched and read many articles about Powershell and replacing text and the need for escaping characters. I thought I did what was needed but nothing gets replaced.
How do I replace the text that includes a back tick?
It looks like that when a replace is used in a batch script and the text contains either a back tick character or a double quote, then all the back ticks and the double quotes need to be escaped in the batch script.
The powershell command that was
powershell -Command "(Get-Content "test.txt") | Foreach-Object {$_ -replace 'name="BLUE_TBL_AC_EA_FREQ"', 'name="BLUE_TBL_ACEAFREQ"'} | Set-Content "testo.txt""
Becomes:
powershell -Command "(Get-Content "test.txt") | Foreach-Object {$_ -replace 'name=\"BLUE_TBL_AC_EA_FREQ\"', 'name=\"BLUE_TBL_ACEAFREQ\"'} | Set-Content "testo.txt""
If there is a back tick in the string then the back tick must be escaped as well using the reverse slash such as
\`
What a mess in a batch script.
Thanks for helping me figure this out.
I think your command have error that's why it is not changed. You missed the double quote after the txt, before the semicolon.
I tried this in my powershell and it is working.
(Get-Content "C:\test.txt") | Foreach-Object {$_ -replace ' name="BLUE_TBL_AC_EA_`FREQ"', ' name="BLUE_TBL_ACEAFREQ"'} | Set-Content "C:\test.txt"
I´m trying to make a Powershell script that reports if there´s a file not older than x hours which contains some string pattern. I made this:
Get-ChildItem C:\Folder -recurse | Select-String -pattern "err" | group path | select name | Where {$_.LastWriteTime -gt (Get-Date).AddHours(-12)}
Problem is that the last part of code which should select only files younger than x hours does not work - shows no files. When I change -gt to -lt it shows every file in the folder which contains pattern including younger than defined hours.
Does anyone has a solution please?
Thank you in advance
Your pipeline is in the wrong order. You are piping a collection of strings to Group-Object, which pipes a different collection to Select-Object, etc. Your call to Where-Object is receiving the output of Select-Object, which is a collection of PSCustomObjects.
What you want is to pipe the file objects themselves to Where-Object, and then pass those file objects down the pipeline:
Get-ChildItem C:\Folder -recurse |
Where {$_.LastWriteTime -gt (Get-Date).AddHours(-12)} |
Select-String -pattern "err" | group path | select name
I admit I am quite tired today but even that is no excuse for the nightmare i seem to be creating for myself with Powershell at present.
Basically the aim is to search a directory for a string that is contained in some word documents. Then I need to return the file name, created date and the last write time. Easy I thought but at some stage I have gone well off track and am still climbing.
What I have done so far is run the search and export the path variable to a text file giving me the path to the files that contain the string. I have then tried to use a foreach loop to load the contents of the file and run a Get-ChildItem against each entry piped to a Select-Object Name,CreationTime,LastWriteTime. I have finally > this to a text file. However it now seems to be returing the info for every file in teh directory and not just those that contain the string I am searching for. I get the feeling I am vastly over complicating this as i tend to with these things. Any help greatly appreciated.
Get-ChildItem -Recurse -Include *.doc | Select-String "Shiba" | select-object path > C:\TRCALM\shibapath.txt
$files = get-content C:\TRCALM\shibapath.txt
foreach($i in $files){gci $i | select-object Name,CreationTime,LastWriteTime > C:\TRCALM\SHIBADates.txt}
You were almost there:
$files = (Get-ChildItem -Recurse -Include *.doc | Select-String "Shiba"| select-object path )
$k = foreach($i in $files){ (gci $i.Path | select-object Name,CreationTime,LastWriteTime) }
$k > C:\pst\SHIBADates.txt