PowerShell - Looping differences

Опубликовано: 25 Февраль 2021
на канале: Mr Automation
352
13

(What are differences between array.foreach and array.where VS foreach and where)
In this video I demonstrate the differences between powershell (.NET) array.foreach({}) and array.where({}) and how they relate (or not) to where-object and foreach-object

*differences between foreach and foreach-object and .foreach
*differences between where-object and .where
*powershell
*learn powershell
*windows
*windows powershell

Code :


$all = Get-ChildItem C:\Windows -Recurse -ErrorAction SilentlyContinue

$res = Measure-Command{ $all.ForEach({ })} | select -ExpandProperty TotalSeconds
Write-Output "all.ForEach took $res"

$res = Measure-Command{ $all | ForEach-Object{ }} | select -ExpandProperty TotalSeconds
Write-Output "ForEach-Object took $res"

$res = Measure-Command{ $all.Where({ }) } | select -ExpandProperty TotalSeconds
Write-Output "all.Where took $res"

$res = Measure-Command{ $all | Where-Object{ } } | select -ExpandProperty TotalSeconds
Write-Output "Where-Object took $res"

$res = Measure-Command{ foreach ($item in $all){} } | select -ExpandProperty TotalSeconds
Write-Output "foreah item in items took $res"

$res = Measure-Command { for($i=0; $i -lt $all.Count; $i++){}} | select -ExpandProperty TotalSeconds
Write-Output "for I took $res"



#(Get-ChildItem C:\Windows\System32 -Recurse -ErrorAction SilentlyContinue).foreach({
$PSItem
#})


$all.Where({
$PSItem.fullname -match "windows"
})

$all | Where-Object {$PSItem.fullname -match "windows"}

foreach($item in $all){
$item.FullName
}

$all | ForEach-Object {
$PSItem.fullname
}

$all.ForEach({
$PSItem.fullname
})


Смотрите видео PowerShell - Looping differences онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал Mr Automation 25 Февраль 2021. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 352 раз и оно понравилось 13 посетителям.