I’m trying to get it to recurse through a directory, and include only .pdf files. Then return the 3 most recently modified .pdf‘s, and stick each of their (full) filenames into their respective variables.
Here’s my code so far –
$Directory="C:\PDFs"
Get-ChildItem -path $Directory -recurse -include *.pdf | sort-object -Property LastWriteTime -Descending | select-object -First 3 | ForEach-Object
{
Write-Host -FilePath $_.fullname
}
However, when I run the script, it asks me to provide parameters for the ForEach portion of the script – Which leaves me to conclude that either the command isn’t piping the way it should, or the command isn’t being using the command properly.
Remove the
enterafter theforeach-object:There’s a typo in your code: