how to get file creation date in powershell - powershell

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

Related

How to count the result of this bat command

I came up with this command to show all the files inside a directory with a specific size:
Get-ChildItem -path Z:\htdocs\zz -recurse | where { $_.Length -eq 2254 }
Now I would like to know HOW MANY results were returned... how do I do that?
Using measure I can count but I cant compare it with a number... for example, i would like to compare it to 3, if it is equal to 3 files I would like to do some stuff.
EDIT
I also want to delete the files. I came up with this code but it is not actually deleting the files AND it does not display at the end the amount of files deleted:
Get-ChildItem -path Z:\htdocs\zz | where { $_.Length -eq 2254 } | ?{Remove-Item $_.fullname -WhatIf}
The Measure-Object cmdlet can count objects.
$objectCount = Get-ChildItem Z:\htdocs\zz -Recurse |
Where-Object { $_.Length -eq 2254 } |
Measure-Object |
Select-Object -ExpandProperty Count
You want the Count property of the Measure-Object output object.
try this:
$(Get-ChildItem -path Z:\htdocs\zz -recurse | where { $_.Length -eq 2254 }).Count
by putting it in brackets and adding the $ you "speak" to the result object and can then use the object function Count

Finding Files in a directory equal to 0 Powershell

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)}

Compare-Object Delete File if file does not exist on source

I have this PowerShell code that compares 2 directories and removes files if the files no longer exist in the source directory.
For example say I have Folder 1 & Folder 2. I want to compare Folder 1 with Folder 2, If a file doesn't exist anymore in Folder 1 it will remove it from Folder 2.
this code works ok but I have a problem where it also picks up file differences on the date/time. I only want it to pick up a difference if the file doesn't exist anymore in Folder 1.
Compare-Object $source $destination -Property Name -PassThru | Where-Object {$_.SideIndicator -eq "=>"} | % {
if(-not $_.FullName.PSIsContainer) {
UPDATE-LOG "File: $($_.FullName) has been removed from source"
Remove-Item -Path $_.FullName -Force -ErrorAction SilentlyContinue
}
}
Is there an extra Where-Object {$file1 <> $file2} or something like that.?
I am not sure how you are getting the information for $source and $destination I am assuming you are using Get-ChildItem
What i would do to eliminate the issue with date/time would be to not capture it in these variables. For Example:
$source = Get-ChildItem C:\temp\Folder1 -Recurse | select -ExpandProperty FullName
$destination = Get-ChildItem C:\temp\Folder2 -Recurse | select -ExpandProperty FullName
By doing this you only get the FullName Property for each object that is a child item not the date/time.
You would need to change some of the script after doing this for it to still work.
If I am not getting it wrong, the issue is your code is deleting the file with different time-stamp as compared to source:
Did you try -ExcludeProperty?
$source = Get-ChildItem "E:\New folder" -Recurse | select -ExcludeProperty Date
The following script can serve your purpose
$Item1=Get-ChildItem 'SourcePath'
$Item2=Get-ChildItem 'DestinationPath'
$DifferenceItem=Compare-Object $Item1 $Item2
$ItemToBeDeleted=$DifferenceItem | where {$_.SideIndicator -eq "=>" }
foreach ($item in $ItemToBeDeleted)
{
$FullPath=$item.InputObject.FullName
Remove-Item $FullPath -Force
}
Try something like this
In PowerShell V5:
$yourdir1="c:\temp"
$yourdir2="c:\temp2"
$filesnamedir1=(gci $yourdir1 -file).Name
gci $yourdir2 -file | where Name -notin $filesnamedir1| remove-item
In old PowerShell:
$yourdir1="c:\temp"
$yourdir2="c:\temp2"
$filesnamedir1=(gci $yourdir1 | where {$_.psiscontainer -eq $false}).Name
gci $yourdir2 | where {$_.psiscontainer -eq $false -and $_.Name -notin $filesnamedir1} | remove-item
If you want to compare files in multiple dir, use the -recurse option for every gci command.

Limiting Powershell Get-ChildItem by File Creation Date Range

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

Powershell, trying to output only the path and lastwritetime on directories

I am trying to write a script that will output any directory that has not changed in over 90 days. I want the script to only show the entire path name and lastwritetime. The script that I wrote only shows the path name but not the lastwritetime. Below is the script.
Get-ChildItem | Where {$_.mode -match "d"} | Get-Acl |
Format-Table #{Label="Path";Expression={Convert-Path $_.Path}},lastwritetime
When I run this script, I get the following output:
Path lastwritetime
---- ----------
C:\69a0b021087f270e1f5c
C:\7ae3c67c5753d5a4599b1a
C:\cf
C:\compaq
C:\CPQSYSTEM
C:\Documents and Settings
C:\downloads
I discovered that the get-acl command does not have lastwritetime as a member. So how can I get the needed output for only the path and lastwritetime?
You don't need to use Get-Acl and for perf use $_.PSIsContainer instead of using a regex match on the Mode property. Try this instead:
Get-ChildItem -Recurse -Force |
? {$_.PSIsContainer -and ($_.LastWriteTime -lt (get-date).AddDays(-90))} |
Format-Table FullName,LastWriteTime -auto
You may also want to use -Force to list hidden/system dirs. To output this data to a file, you have several options:
Get-ChildItem -Recurse -Force |
? {$_.PSIsContainer -and ($_.LastWriteTime -lt (get-date).AddDays(-90))} |
Select LastWriteTime,FullName | Export-Csv foo.txt
If you are not interested in CSV format try this:
Get-ChildItem -Recurse -Force |
? {$_.PSIsContainer -and ($_.LastWriteTime -lt (get-date).AddDays(-90))} |
Foreach { "{0,23} {1}" -f $_.LastWriteTime,$_.FullName} > foo.txt
Also try using Get-Member to see what properties are on files & dirs e.g.:
Get-ChildItem $Home | Get-Member
And to see all values do this:
Get-ChildItem $Home | Format-List * -force