When I filter some folders and output to a html file, the path in the result is always empty.
I can't find why it only works on files but folders?
Get-ChildItem -Recurse $source -Filter *PML_*_ECR* | where { $_.psiscontainer } | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-6)} | sort LastWriteTime -descending | select name,LastWriteTime,Directory | convertto-html -head $a -body "<H2>Folder LIST FOR PAST 7 DAYS </H2>" | out-file $output\results.htm
Folders are represented as DirectoryInfo objects, which don't have a Directory property. The full path of the folder object itself is provided via the FullName property:
... | select Name, LastWriteTime, FullName | ...
The path of the parent folder can be obtained via the Parent property:
... | select Name, LastWriteTime, #{n='Directory';e={$_.Parent.FullName}} | ...
Because Directory is not a property of that object. Try doing:
Get-ChildItem -Recurse $source -Filter *PML_*_ECR* | where { $_.psiscontainer } ||GM
Then look at the available properties. I think FullName may better suite your needs.
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'm attempting to find the latest version of multiple files within a directory. Currently, I'm calling GCI per file, but that is extremely slow, so I want to instead cache all the results by unique file name and then just perform a lookup in the cache.
I'm currently doing the following:
Gci $filePath -Recurse | ?{ -Not $_.PSIsContainer } | Group-Object Name
I'm trying to convert this to the powershell equivalent of the C# code:
group.ToDictionary(g => g.Key, g => g.Values.OrderByDescending(v => v.ModifiedAt).First().FullName)
How would I accomplish this in Powershell?
What I would do to create a hashtable of files would be to create an empty hashtable, and then populate it with the results of your GCI:
$files = ${}
GCI $filepath -Recurse -File | Group Name | ForEach{ $files.Add($_.Name, ($_.Group | Sort LastWriteTime)) }
Or if all you want is the most recent file, add | Select -Last 1 after the Sort LastWriteTime. If all you care about is the path, you could even do | Select -Last 1 -ExpandProperty FullName.
$Files = ${}
GCI $filepath -recurse | Group Name | ForEach{ $files.Add($_.Name, ($_.Group | Sort LastWriteTime | Select -Last 1 -ExpandProperty FullName)) }
I am working on a script to list all the files with a specific extension (.dll) in this case. my script is working fine except i want to filter out all of those files which have microsoft's copyright. What approach should be taken ?
$Dir = Get-ChildItem C:\Windows\Microsoft.NET\Framework -include *.dll -recurse | sort-object name | format-table name, directory -auto
$Dir
Filter using $_.VersionInfo.LegalCopyright inside a Where-Object-statement. Ex:
$Dir = Get-ChildItem C:\Windows\Microsoft.NET\Framework -include *.dll -recurse |
Where-Object { $_.VersionInfo.LegalCopyright -notmatch 'Microsoft' }
$Dir | sort-object name | format-table name, directory -auto
Never store data from Format-Table in a variable. It throws away the objects and returns unusable format-objects. Only use it when outputing to console or with ex. | Out-String | Out-File ... when saving to a file.
I have a file directory which contains approx. 600 employee image files which have been copied from an alternative source.
The filename format is:
xxxxxx_123456_123_20141212.jpg
When the employee image file is updated it just creates another file in the same location and only the datetime changes at the end.
I need to be able to identify the most recent file, however i need to establish first of all which files are 'duplicated'.
My initial thoughts were to try and match the first 14 characters and, if they matched, work out the recent modified date and then delete the older file.
This requires PowerShell version 3.
$Path = 'C:\Users\madtomvane\Documents\PowerShellTest'
#Get the files #Group them by name #Select the most resent file
$FilesToKeep = Get-ChildItem $Path -Recurse -File | Group-Object -Property {$_.Name[0..14]} | ForEach-Object {$_.Group | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1}
#Get the files #Group them by name #Where there is more than one file in the group #Select the old ones
$FilesToRemove = Get-ChildItem $Path -Recurse -File | Group-Object -Property {$_.Name[0..14]} | Where-Object {$_.Group.Count -gt 1} | ForEach-Object {$_.Group | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 1}
$FilesToRemove | Remove-Item
I'm trying to write a one-liner in Powershell that lists all filetypes in a folder (and its sub-folders).
I've got this so far:
Get-ChildItem D:\Audio -Recursive | Select-Object PSParentPath, Extension | Export-Csv D:\Test.csv
However, I'd like to do better and display, for each folder, every found extension only once.
For example, let's say I have ten mp3 files and 1 jpg file in D:\Audio\foo and 5 flac files and 1 txt file in D:\Audio\bar. I'd like the output to be :
foo .mp3
foo .jpeg
bar .flac
bar .txt
I guess I should use Get-Unique but how do I specify it on the Extension property and NOT on the Path property?
Just add -Unique to Select-Object:
Get-Childitem -Recurse | Select-Object PSParentPath,Extension -Unique
(Also, DirectoryName might be better than PSParentPath here)
i complet solution of Jimmeh, use -file for take only file
Get-ChildItem "c:\temp" -Recurse -file | select directoryname, Extension -Unique
Try this:
Get-ChildItem D:\Audio -Recurse | Group-Object psparentpath, extension | ? {(($_.Name -split ", .")[1]) -ne $Null } | ft -AutoSize #{Expression={(($_.Name.Split("\"))[$_.Name.Split("\").Length -1] -split ",")[0]};Label="Folder"}, #{Expression={($_.Name -split ", .")[1]};Label="Extension"}
To show full path to folder + extension, try the following.
Get-ChildItem D:\Audio -Recurse | Group-Object psparentpath, extension | ? {(($_.Name -split ", .")[1]) -ne $Null } | ft -AutoSize #{Expression={($_.Name.Replace("Microsoft.PowerShell.Core\FileSystem::","") -split ", .")[0]};Label="Folder"}, #{Expression={($_.Name -split ", .")[1]};Label="Extension"}