PowerShell 7 Tutorials for Beginners #8 : ForEach (Loops)
Вставка
- Опубліковано 7 лют 2025
- In this video I will go over using a ForEach Loop in PowerShell 7.2 , we will see what a loop is and compare it against writing out each line out. We will also see 3 different ways to initiate a foreach loop in PowerShell and why it is very useful to use loops in programming/scripting.
Tags:
PowerShell, jackedprogrammer, powershell 7, how to install powershell 7, powershell 7 on vs code, visual studio code
thank you so much. i'm learning powesrshell so i can write scripts for cyversecurity tasks. this is awesome content
I think there is no one better than you on UA-cam!
Thank you so much!
I love the format of your videos. I've been learning PowerShell for a few months and your teaching style works so much better than other courses I've learned from. After watching your other videos, I was able to understand how to create the first foreach loop before you explained it. Keep up the good work and thank you!
I’m appreciative of these for sure
This is the best I have ever seen on youtube these years!!
Really big help on these tutorial videos. Thanks! I've now also learned how to delete multiple folders with the ForEach method as well.
Another great learning video thanks Jack
I tend to use the second method over the first, as that allows begin, process, end
Thanks
Top youtuber!!! 👋 👋
Top youtuber!!!
Could you do a future video on a foreach loop that includes if else statements and PnPBatch? I've been scouring the internet for help with a script I'm writing to no avail.
Hi Jack I have a question I have folder I want to synce for 15 users in sharepoint site , I have there username and password how do I go about scripting from my machine to make sure that folder is there when they sign into a device I’m new to script but I want to learn how to do it any guide would be appreciated
I don't speak English well, sorry, I use a Google translator.
I didn't understand where the $name came from here: foreach($name in $folderNames){... Tell me, please, what is $name means? I mean, we didn't set it as a variable before.
the $name is a temporary variable that is assigned to the element of the array at which the foreach-loop is currently at, for example if the array is @(1,2,3), in the first iteration of the loop $name is assigned to 1, in the second iteration $name is assigned to 2 and so on
I tested it and shows first one is the fastest
#Fastest (1.13s)
foreach($no in 1..1000000){$no}
# (4.5s)
(1..1000000).ForEach( {$_} )
#slowest (9s)
1..1000000 | ForEach-Object { $_ }
i used measure-command to test it.
This is correct, if I were to make this again i would be recommeding the foreach($x in $array) as the fastest, measure-command is super useful good catch! :)
$filePath="C:\Scripts\8 - ForEach\Data\FolderNames.txt"
$foldersPath."C:\Scripts\8 - ForEach\Data\Share"
$folderNames=Get-Content -Path $filePath
foreach($name in $folderNames){
if((Test-Path -Path "$foldersPath\$name") -eq $false){
New-Item -Path "$foldersPath" -Name $name -ItemType Directory
}else{ Write-OutputI"Folder exists"
}
}
$folderNames | ForEach-Object -Process {
if((Test-Path -Path "$foldersPath\$_") -eq $false){
New-Item -Path "$foldersPath" -Name $_ -ItemType Directory
}else{ Write-Output "Folder exists" }
}
$FolderNames.ForEach(
{
if((Test-Path -Path "$foldersPath\$_") -eq $false){
New-Item -Path $foldersPath -Name $_ -ItemType Directory
}else{
Write-Output "Floder exit"
}
}
)