Hello I'm new to Powershell and created a fairly complex code for myself and found out that their's more than one way that I could have written it for example:
$DayLimit = (Get-Date).AddDays(-8)
$Date = Get-Date -Format yyyyMMdd
$Path = Get-ChildItem G:\VLTNAS1_Backup\Active\Nightly\ -Force |
Where-Object { $_.PSIsContainer -eq $True -and $_.CreationTime -lt $DayLimit }
or
ForEach ($Folder in $Path)
{
if($Folder.PSIsContainer -eq $True -and $Folder.CreationTime -lt $DayLimit)
{
Write-Host $Folder
}
}
I Have 2 questions, the first is that I tested both methods out using Write-Host and they print the results differently.
The very first example with the conditions being nested into the variable is that they got printed in a single row for example: Test1 Test2 Test3
The second method I used the results got printed in a single column for example:
Test1
Test2
Test3
Why is that?
My last question is what is the difference from using these two methods, is creating conditions nested within the variable more powerful or vice-versa. I'm just trying to learn more about powershell and accept any help or critique, Thanks.