How to export Txt/Text file to new folder? - powershell

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"

Related

PowerShell cmdlet to get a name of recently zipped folder?

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

Need to put this into a loop

I have a piece of code that works but I not very elegant, it looks at a directory full of backups, deletes all but the last modified.
$path = "C:\Users\test"
Get-ChildItem "$path\*.*" -include *.data* |
Sort-Object -Descending -Property LastWriteTime |
Select-Object -Skip 1 |Remove-Item -Force
Get-ChildItem "$path\*.*" -include *.enroll* |
Sort-Object -Descending -Property LastWriteTime |
Select-Object -Skip 1 |Remove-Item -Force
Get-ChildItem "$path\*.*" -include *.govern* |
Sort-Object -Descending -Property LastWriteTime |
Select-Object -Skip 1 |Remove-Item -Force
I tried this but it does'nt work,
$sites = #("data","govern","enroll")
$path = "C:\Users\test"
for ($i=0; $i -lt $sites.Length; $i++) {
Get-ChildItem "$path\*.*" -include "*.$sites[$i]*" |
Sort-Object -Descending -Property LastWriteTime |
Select-Object -Skip 1 |Remove-Item -Force
}
Just a nice elegant loop is all I'm looking for.

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...

Getting path of a selected item in PowerShell

I am trying to get a path of a folder.
Here is what i got. This code checks latest file and selects it.
$dir = "C:\Users\%PATH%\downloads"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.path
I need to check the path of the file to use it later on to move this folder using Move-Item function.
This is what you are looking for:
$latest.directoryname will give you the path for the same and you can store that in the variable and use it in move-item whenever required.
$dir = "C:\Users\Ranadip\Downloads"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$destination= $latest.DirectoryName
Move-Item C:\scripts\temp* $destination