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
Related
I want a code snippet to put into my profile that will always select the most recent folder.
(gci C:\Users\$env:username\Documents\releases | ? { $_.PSIsContainer } | sort CreationTime | Select-Object Name)[-1]
This is my output.
Name
----
20201116_124047
I would like the output to be a string that I can place into a variable. I am on Powershell 5.1
20201116_124047
try this :
$myvar=Get-ChildItem "C:\Users\$env:username\Documents\releases" -Directory | sort CreationTime -Descending | Select -ExpandProperty Name -First 1
The property I think you need is LastWriteTime, instead of CreationTime.
Also, unless you are using a PowerShell version below 3.0, you can use the -Directory switch and do not have to use Where-Object { $_.PSIsContainer }
Get-ChildItem -Path "C:\Users\$env:username\Documents\releases" -Directory |
Sort-Object LastWriteTime |
Select-Object -ExpandProperty Name -Last 1
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
Suppose we have two directories C:\username\test1 & C:\username\test2. Both directories contain same file script.ps1. Now with powershell script I want to search the file script.ps1 in both directories & want the complete file location of file which is latest modified/created.
I was using below command but it did not give the desired output
Get-ChildItem -Path "C:\username" script.ps1 -Recurse | Where-object {!$_.psIsContainer -eq $true} | ForEach-Object -Process {$_.FullName} | select -last 1
For a given directory you can use
Get-ChildItem C:\dir1\dir2 -Recurse -ErrorAction SilentlyContinue | Where {!$_.PsIsContainer}|select Name,DirctoryName, LastWriteTime |Sort LastWriteTime -descending | select -first 1 Name DirctoryName LastWriteTime
And if you want it to run for multiple directories, you will have to run a loop on each directory:
Get-ChildItem C:\dir\* | Where {$_.PsIsContainer} | foreach-object { Get-ChildItem $_ -Recurse -ErrorAction Sile ntlyContinue | Where {!$_.PsIsContainer} | Select Name,DirectoryName, LastWriteTime, Mode | Sort LastWriteTime -descend ing | select -first 1}
It will list files which are last modified for each directories.
Edit: Search for a file
You can use following command to search for a file recursively if it is there in multiple directories:
Get-ChildItem -Path C:\Myfolder -Filter file.whatever -Recurse -ErrorAction SilentlyContinue -Force
This will list all versions of the file found, from newest to oldest:
Get-ChildItem -Path "C:\UserName" `
-File `
-Recurse `
-Include "Script.ps1" |
Sort-Object LastWriteTime -Descending |
Format-Table LastWriteTime, FullName -AutoSize
If you only want the most recent one, then replace the Format-Table line with:
Select-Object -First 1
I have script that selects .exe files with the specified name from the local folder and removes all files, except first.
$P variable is defined in param.
$P ="$($env:USERPROFILE)\Desktop\I"
Then I got this error
$C = Get-ChildItem $P -Filter *.exe| Where-Object Name -Like '*r_2-2*' | Sort-Object Name -Descending | Select-Object -ExpandProperty Name -Skip 1 | Remove-Item
Remove-Item : Cannot find path 'D:\FM\r_2-2.exe' because it does not exist.
At line:1 char:251
+ ... Descending | Select-Object -ExpandProperty Name -Skip 1 | Remove-Item
I know about foreach loop but want to use For-EachObject cmdlet instead.
You were quite close, if you want to use ForEach-Object:
Get-ChildItem $P -Filter *.exe | Where-Object Name -Like '*r_2-2*' | Select-Object -Skip 1 | ForEach-Object { remove-item $_.FullName -force }
To skip one first found result just Select-Object -Skip 1 is enough.
Remove-Item -Force also removes hidden and read-only files.
You can make the use of FullName parameter directly in your statement. Try this -
$C = Get-ChildItem $P -Filter *.exe| Where-Object Name -Like '*r_2-2*' | Sort-Object Name -Descending | Select-Object -ExpandProperty FullName -Skip 1
$c | ForEach-Object {Remove-Item -Path $_}
Use -Force parameter if you want to delete the hidden files too.
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...