I use a Powershell command to generate a CSV report of certain file types. My goal is to find out how many were added during a particular date range. Right now, the script finds everything and I sort by date to find my number. I'd like to modify the command to only return objects within a creation date rage, i.e. if this file was created between 1 March 2013 and 31 March 2013. There's probably a way to limit the command by a date range, likely using Select-Object, I just can't figure it out.
Get-ChildItem 'PATH' -recurse -include #("*.tif*","*.jp2","*.pdf") | Select-Object FullName, CreationTime, #{Name="Mbytes";Expression={$_.Length/1Kb}}, #{Name="Age";Expression={(((Get-Date) - $_.CreationTime).Days)}} | Export-Csv 'PATH\scans.csv'
Use Where-Object and test the $_.CreationTime:
Get-ChildItem 'PATH' -recurse -include #("*.tif*","*.jp2","*.pdf") |
Where-Object { $_.CreationTime -ge "03/01/2013" -and $_.CreationTime -le "03/31/2013" }
Use Where-Object, like:
Get-ChildItem 'PATH' -recurse -include #("*.tif*","*.jp2","*.pdf") |
Where-Object { $_.CreationTime -gt "03/01/2013" -and $_.CreationTime -lt "03/31/2013" }
Select-Object FullName, CreationTime, #{Name="Mbytes";Expression={$_.Length/1Kb}}, #{Name="Age";Expression={(((Get-Date) - $_.CreationTime).Days)}} |
Export-Csv 'PATH\scans.csv'
Fixed it...
Get-ChildItem C:\Windows\ -recurse -include #("*.txt*","*.pdf") |
Where-Object {$_.CreationTime -gt "01/01/2013" -and $_.CreationTime -lt "12/02/2014"} |
Select-Object FullName, CreationTime, #{Name="Mbytes";Expression={$_.Length/1Kb}}, #{Name="Age";Expression={(((Get-Date) - $_.CreationTime).Days)}} |
Export-Csv C:\search_TXT-and-PDF_files_01012013-to-12022014_sort.txt
Related
I am trying to get file creation date into a variable in powershell, however unable to do so. The "$_.CreationTime" just prints the string literal ".CreationTime". How to get actual creation time of file?
$builds = Get-ChildItem "$path_to_directory" *.zip | Where-Object {$_.CreationTime -gt $lastBuildDeployedTimestamp}
foreach($build in $builds)
{
"$path_to_directory"
"$_.CreationTime"
}
Use "$($_.CreationTime)" .
In your particular example it should be "$($build.CreationTime)"
A one liner approach would be
Get-ChildItem $path_to_directory *.zip | Where-Object {$_.CreationTime -gt $lastBuildDeployedTimestamp} | Select-Object -Property FullName, CreationTime
However, if you'd like to keep your loop then you'll need to use $build.
$builds = Get-ChildItem $path_to_directory *.zip | Where-Object {$_.CreationTime -gt $lastBuildDeployedTimestamp}
foreach($build in $builds)
{
$path_to_directory
$build.CreationTime
}
For more info see Get-Help about_Foreach -Full
I'm trying to open the most recent picture from all my folders in my onedrive using powershell. I have already gotten to the point where i have a "list" which automatically sorts to newest and only gives me the "first 1" result.
I've tried implementing "Select-Object ().Open" but i don't see where i need to implement it. I can't get it to work.
I've also tried taking the directory and filename in variables to try and paste these together to then use these to open the picture.
This is my command:
Get-ChildItem -Recurse | Where-Object {$_.Name -match "(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge "03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1
I'm expecting that there is a way to do this action and open this file in 1 command.
EDIT: I just discovered "Fullname" in the "Select-Object" function which is my directory and filename togheter. I still don't know how to implement ".Open" with this though.
Try this -
$FirstFile = Get-ChildItem -Recurse | Where-Object {$_.Name -match "(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge "03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1
Start-Process $FirstFile.FullName
You can also put the whole thing into a single line, though breaking the code will provide more readability -
Start-Process (Get-ChildItem -Recurse | Where-Object {$_.Name -match "`
(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge`
"03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1).FullName
I was wondering how I can display a list of empty files in a directory
$test = gci "C:\Users\Freedom\Documents" -Recurse
$test | Where-Object {$_.PsISContainer} | Select-Object FullName | Where-Object {$_.GetFiles() -eq 0}
I Don't understand because when I do get-childitem | get-member I get a list of properties and methods I can use and in the list is getfiles() why can't I use this method why's it giving me an error message?
Method invocation failed because [System.IO.FileInfo] does not contain a method named 'GetFiles'.
I think you want this:
Get-ChildItem | Where-Object { (-not $_.PSIsContainer) -and ($_.Length -eq 0) }
If you have PowerShell 3.0 or later you can use this:
Get-ChildItem -File | Where-Object { $_.Length -eq 0 }
Of course you can add whatever other parameters for Get-ChildItem that you want (-Recurse, etc.).
Wow I had what I wanted mixed up! And I had to add the .count to the getfiles() method
$test | Where-Object {$_.PsISContainer} | Where-Object {$_.GetFiles().Count -eq 0} | Select-Object FullName
try this
Get-ChildItem "c:\temp" -File -Recurse | where Length -eq 0
Use Get-ChildItem and the File flag, -Recurse is needed to get every file in the folder and in the folder below. Then get all the files were Get-Content returns null.
Get-ChildItem $YourPath -Recurse -File | Where-Object {!(Get-Content $_.Fullname)}
I need to have a clean-up script that remove all files older than 30 days but if file is older than 30 days it should save the last one. Possible? :)
I have tried a couple of parameters but cannot really get it to work.. guess I need a if/else clause?
Would appreciate any guide and help with this, thanks
$Daysback = "-30"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
$path = "C:\Data\*"
$save1 = Get-ChildItem -Path $path | Where-Object {($_.Name -like "Test*.zip")} | sort LastWriteTime -Descending | select -First
Get-ChildItem $path -Recurse
{($_.CreationTime -le $(Get-Date).AddDays($Daysback))}
{
Remove-Item -Recurse -Force
}
elseif ($save1)
{
Remove-Item -Recurse -Force
}
}
Something like this should work.
$Daysback = "-30"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
$path = "C:\Data\*"
$Items=Get-ChildItem -Path $path -Recurse | Where-Object {($_.Name -like "Test*.zip") -and ($_.LastWriteTime -le ($DatetoDelete))}| Sort-Object LastWriteTime -Descending
$Items|Select-Object -Skip 1 |Remove-Item -Recurse -Force -Path $_.fullname
Get-ChildItem -> Filter, only get the items that name starts with Test and ends with .Zip that were written over 30 days ago. Sort them.
In the delete line, we use -Skip 1 to skip over the first item in the sorted list and remove the items by using their path.
This can be simplified. The below block will grab all files in C:\Data that meet the filter (faster than Where-Object significantly), then further reduces those based on their CreationTime, skips 1, and deletes the rest.
Get-ChildItem -Path 'C:\Data' -Filter 'Test*.zip' -Recurse |
Where-Object { -not $_.PSIsContainer -and
$_.CreationTime -le (Get-Date).AddDays(-30) } |
Sort-Object -Property 'LastWriteTime' -Descending |
Select-Object -Skip 1 |
Remove-Item -Force -WhatIf
I was wondering if is possible to specify multiple search filters as once. For example, I have this line of code that finds all files that have the "&" symbol.
get-childitem ./ -Recurse -Filter "*&*" |
? { $_.PSIsContainer } |
Select-Object -Property FullName
I'd like to extend this so that I can search one time and find files with other symbols like %,$,#, etc. I want to find files that have any of these symbols, not neccesarly files that have all of them so I assume there needs to be an OR somewhere. I tried the below code but it didn't seem to work for me:
get-childitem ./ -Recurse -Filter "*&*" -Filter "%" |
? { $_.PSIsContainer } |
Select-Object -Property FullName
You can use the -match operator and a regex for this:
Get-ChildItem -Recurse |
Where { !$_.PSIsContainer -and ($_.name -match '&|%|\$|#')} |
Select-Object -Property FullName
If you are on PowerShell v3 or higher you can simplify this a bit:
Get-ChildItem -Recurse -File |
Where Name -match '&|%|\$|#' |
Select-Object -Property FullName
If you have V3 or better, you can leverage the "globbing" wildcard feature:
get-childitem './*[&%$#]*' -Recurse | where {$_.PSIsContainer}
If you've got V4, you can dispense with the $_.PSIsContainer filter and use the -Directory switch:
get-childitem './*[&%$#]*' -Recurse -Directory