PowerShell cmdlet to get a name of recently zipped folder? - powershell

I have tried
$latest1 = gci $path -Include *.zip| ? { $_.PSIsContainer } | sort CreationTime -desc | select -f 1
but $latest1 is giving blank output.

The -Include switch will only work if the path ends in \* or when used together with the -Recurse switch.
Since in your case, you are only looking for zip files, I would use the -Filter parameter.
$latest1 = Get-ChildItem $path -Filter '*.zip' -File |
Sort-Object CreationTime -Descending | Select-Object -First 1
For PowerShell version below 3.0 use
$latest1 = Get-ChildItem $path -Filter '*.zip' | Where-Object { !$_.PSIsContainer } |
Sort-Object CreationTime -Descending | Select-Object -First 1
If a zip file was found in the path, $latest should now be a FileInfo object with properties like FullName, BaseName etc.

Try changing your where-object filter or removing it
$latest1 = gci $path -Include *.zip| ? { $_.PSIsContainer -eq $false } | sort CreationTime -desc | select -f 1
$latest1 = gci $path -Include *.zip| | sort CreationTime -desc | select -f 1

Related

Recursively looking for count of file extension using PowerShell

Looking to get a PowerShell snippet that will list all file extensions recursively in a folder along with the count.
/abc/(1).cfc
/abc/john/(265).cfm, (1).html
/abc/john/js/(25).js
/abc/john/css/(6).css
This is what I've tried so far, but I'm not sure how to add the path for each:
Get-Childitem "C:\projects\newfolder\edit" -Recurse |
where { -not $_.PSIsContainer } |
group Extension -NoElement |
sort count -Desc
The answer posted by #AnsgarWiechers is OK if you want the list of matching files as well as the count; if you only want the directory name and the count,
(Powershell v2:)
Get-ChildItem 'C:\projects\newfolder\edit' -Recurse |
Where-Object { -not $_.PSIsContainer } |
Group-Object DirectoryName, Extension |
Select-Object Name, Count
(PowerShell v3+:)
Get-ChildItem 'C:\projects\newfolder\edit' -Recurse -File |
Group-Object DirectoryName, Extension |
Select-Object Name, Count
You can group by multiple criteria:
Get-ChildItem 'C:\projects\newfolder\edit' -Recurse |
Where-Object { -not $_.PSIsContainer } |
Group-Object DirectoryName, Extension
On PowerShell v3 and newer the filtering can be done directly with Get-ChildItem:
Get-ChildItem 'C:\projects\newfolder\edit' -Recurse -File |
Group-Object DirectoryName, Extension
You can use the following script to achieve the expected output:
Get-ChildItem "C:\TempFiles" -Recurse -File | Group-Object DirectoryName |
ForEach-Object {
"{0}\{1}" -f $_.Name,
(($_.Group | Group-Object Extension |
ForEach-Object { ("({0}){1}" -f $_.Count, $_.Name) }) -join ",")
}
Output
C:\TempFiles\Root\(1).json,(2).html,(2).txt
C:\TempFiles\Root\Folder1\(4).json,(2).html,(2).txt,(2).png
C:\TempFiles\Root\Folder1\Folder1-1\(2).json,(2).html
C:\TempFiles\Root\Folder2\(2).txt

powershell, check a backup directory and delete old ones only if there is more than one file

Hello to the whole community, I am trying to inspect directories and subdirectories of a folder and if one of them gets more than one file if it has more than 15 days to delete it and leave only the most updated.
but I still do not get the way that if I get a single file despite having more than 15 days old do not touch it as long as there is one more updated within the same directory.
I am currently working with this code
$timeLimit = (Get-Date).AddDays(-15)
Get-ChildItem D:\backup\OldFilesTemp -Directory | where LastWriteTime -lt $timeLimit | Remove-Item -Force -Recurse
grateful for the support they can give me.
You could try something like the following:
$timeLimit = (Get-Date).AddDays(-15)
Get-ChildItem D:\backup\OldFilesTemp | Where-Object { $_.PSIsContainer } | ForEach-Object { Get-ChildItem $_ | Where-Object { -not $PSIsContainer } | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 1 | Where-Object { $_.LastWriteTime -lt $timeLimit } | Remove-Item -Force }
Replace Remove-Item -Force with Remove-Item -WhatIf to perform a dry run.
$timeLimit = ([System.DateTime]::Today).AddDays(-15) #Dont use Get-Date.
$BackupFolder = "D:\backup\OldFilesTemp"
$FolderList = Get-ChildItem $BackupFolder -Directory -Recurse | Select FullName
Foreach ($Folder in $FolderList)
{
$FileList = Get-ChildItem $Folder -File | Sort-Object -Property LastWriteTime -Descending
$Count = ($FileList | Where-Object -Property LastWriteTime -GE $timeLimit).Count
#Keep an old file if there is only 1 or no recent backups
if ($Count -le 1)
{
$FileList | Where-Object -Property LastWriteTime -LT $timeLimit | Select-Object -Skip 1 | Remove-Item -Force
}
else
{
$FileList | Where-Object -Property LastWriteTime -LT $timeLimit | Remove-Item -Force
}
}
Better do your testing before you deploy on your environment.

Powershell select most recent file containing a specific string

I have written a code in powershell that selects the most recent file in a directory.
$first = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1
$first.name
However, I need to select the most recent file containing a specific string in the name. How can I adapt my code in order to do this?
I got it to work using this:
$filterIRP1064="IRP_1064*"
$latest1064 = Get-ChildItem -Path $dir -Filter $filterIRP1064 | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest1064.name
#Michael Hoffmann
Like this?
$first = Get-ChildItem -recurse | Select-String -pattern "stringhere" | group path | select name
Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1
$first.name
Get-ChildItem -recurse | Select-String -pattern "stringhere" | group path | select name
Use this to get all the files containing your string. Select the most recent one afterwards.
Get-ChildItem -path $dir | Select-String -pattern "stringhere" | group path | Sort-Object CreationTime -Descending | Select-Object -First 1 | select name
This should work...

powershell script statement to fetch a particular file path

I have the following statement in PowerShell script that fetches a particular file
$imptext = "E:\imp\old"
$strAttachment = dir $imptext | sort -prop LastWriteTime | Where-Object {$_.name -like "*Imptextfiles*"} | Where-Object {$_.lastwritetime -gt (Get-Date).AddHours(-1)} | select -last 1 | foreach-object -process { $_.FullName }
How can I write to host the path of that file $strAttachment along with the exact file name?
You want to get the directory of the file? Just use the Split-Path cmdlet:
$attachmentDirectory = Split-Path $strAttachment
You could also use Get-DirectoryName:
$attachmentDirectory =[System.IO.Path]::GetDirectoryName($strAttachment)
Also, you can improve your inital script (using -Filter instead of two Where-Object)
$strAttachment = Get-ChildItem $imptext -Filter '*Imptextfiles*' |
Where-Object Lastwritetime -gt (Get-Date).AddHours(-1) |
sort LastWriteTime |
select -last 1 |
select -ExpandProperty FullName

How to export Txt/Text file to new folder?

This code does taking newest text file from the folder.
$dir = "C:\logsnew\Application"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name
showing like this
C:\Users\kimi> $dir = "C:\logsnew\Application"
C:\Users\kimi> $latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
C:\Users\kimi> $latest.name
4552-4084-63585921993.txt
I would like to put this newest txt file "4552-4084-63585921993.txt" to new folder name "logstop1".
So I try like this:
$dir = "C:\logsnew\Application"
Get-ChildItem -Path $dir |
Sort-Object LastAccessTime -Descending |
Select-Object -First 1 |
Add-Content C:\logsTop1
but this error happens:
Add-Content : Access to the path 'C:\logsTop1' is denied.
How can I fix this problem?
Use Move-Item instead of Add-Content if you want to move the file or Copy-Item if you want to copy it.
Get-ChildItem -Path "C:\logsnew\Application" | Sort-Object LastAccessTime -Descending | Select-Object -First 1 | Move-Item -Destination "C:\logspath1"