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
Related
Is it possible to use any sort of diff utility to diff based on filename only, excluding the file extensions? I have multiple dirs that have various versions of a file, ie media.mov, media.mp4, media.jpg, etc. I want to make sure all versions were made for each file (1000s). So /dir1/media_99.mov and /dir2/media_99.mp4 would yield a TRUE condition. Man diff does not have "--ignore-extension" and I'm not sure how to possibly use "--exclude-from=FILE". I can use Linux (preferred) or PowerShell (if I must)
In PowerShell, if you want to know which file names are unique to /dir1, use a Compare-Object call, followed by reducing those file names to their base name (file name without extension), weeding out duplicates, and sorting via Sort-Object
Compare-Object -PassThru -Property Name (Get-ChildItem -File /dir1) (Get-ChildItem -File /dir2) |
Where-Object SideIndicator -eq '<=' |
ForEach-Object BaseName |
Sort-Object -Unique
Note: The assumption is that both Get-ChildItem calls return at least one file-info object, otherwise the Compare-Object call will fail - guard against that with if statements, if necessary.
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
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"
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