How to use PowerShell foreach

  Рет қаралды 41,939

Shane Young

Shane Young

Күн бұрын

Пікірлер: 58
@b3nisrael
@b3nisrael 6 жыл бұрын
No article on the whole Internet has explained the ForEach as good as this video Shane, please please keep them coming.
@ShanesCows
@ShanesCows 6 жыл бұрын
I am blushing. :)
@MuazAlnajjar
@MuazAlnajjar 5 жыл бұрын
thank you Simpleton you saved me the time writing the same statement. thank you Shane!!
@michaelbartnicki9464
@michaelbartnicki9464 4 жыл бұрын
what really helped me was how you really took the time to explain how $object variable worked. The way i understood it was basically when you start a foreach statement that tells powershell that whatever the first variable in the parentheses is, is basically the variable to be able to cycle through all the objects in the previously declared variable such as $ourobjectsarray
@ShanesCows
@ShanesCows 4 жыл бұрын
Glad to help. 😀
@charlesandrews6643
@charlesandrews6643 2 жыл бұрын
I learnt my first foreach script. Thank you
@ShanesCows
@ShanesCows 2 жыл бұрын
Awesome 🤩
@gerardguinee6450
@gerardguinee6450 4 жыл бұрын
You saved my day, listening into first 4 minutes i saw where I made a mistake in my own baked ps1 script. Thanks.
@ShanesCows
@ShanesCows 4 жыл бұрын
Awesome 😎
@talosvalcoran4714
@talosvalcoran4714 4 жыл бұрын
Super awesome explanation. Since I have found your videos I have made way and way more progress than from the book alone that I bought. I'll try to put as much of what I learn into actual practice.
@ShanesCows
@ShanesCows 4 жыл бұрын
Great to hear!
@eivenhoe
@eivenhoe 3 жыл бұрын
Always informative, compact and good stuff, thanks Shane for sharing your knowledge
@ShanesCows
@ShanesCows 3 жыл бұрын
Glad to help. 😀
@TiteufMela
@TiteufMela 2 жыл бұрын
Amazing videos as usual! Do you have any powershell series related to directories, files and file servers, permissions...?
@lightalwayz
@lightalwayz 3 жыл бұрын
This is a great series on powershell. Thank you Shane. Do you have training on Active Directory - RBAC?
@ShanesCows
@ShanesCows 3 жыл бұрын
Thanks. I don’t do rbac. Sorry b
@73Jes
@73Jes 3 жыл бұрын
Have to say that has finally got for each working in my brain, the whole $object $dog thing really worked, that was the bit I could never figure out
@ShanesCows
@ShanesCows 3 жыл бұрын
Awesome
@550891
@550891 4 жыл бұрын
very helpful as always. thank you.
@ShanesCows
@ShanesCows 4 жыл бұрын
My pleasure!
@tendyfish
@tendyfish 5 жыл бұрын
thank you for a well explained video. that $object/$dog variable had me pulling my hair off everytime!!
@ShanesCows
@ShanesCows 5 жыл бұрын
Dogs will do that to you. ;)
@timepassguys432
@timepassguys432 2 жыл бұрын
Hi, thanks. This helped me a lot. I have an question, i have an similar scenario, i have an method which will invoke an restapi. In the main function i have added the foreach loop with a CSV file while includes two values (.id1 and .id2) which should be passed into rest API request. I'm getting 502 bad gateway error while doing that. The same API works fine on postman with collection runner
@academyministorage6397
@academyministorage6397 2 жыл бұрын
Why did you separate the brackets to individual lines between Line 22 and 24?
@sufferinglamb
@sufferinglamb 5 жыл бұрын
Thanks Shane, great video. Do you have any tutorials that show us how to add a new property/value into an object during a ForEach?
@ShanesCows
@ShanesCows 5 жыл бұрын
I don’t. Sorry
@michaelcook7389
@michaelcook7389 5 жыл бұрын
I'm assuming you've probably figured out how to do that by now. Your question carries too many assumptions. But, seems like a good one. I've got an answer for you. Let's say you want to add a property during a loop. Is it a property that you need to create or index during a loop? If so, that's pretty easy. You have to create an index array first. $Item = @( 0..number of entries you want to have ) Then you have to iterate through those indexes to add everything. Foreach ( $i in 0..( $item.count - 1 ) ) # this sets up a numerical anchor/index to reference within the loop { $item[$_] = @{ Property1 = "Value1" ; Property2 = "Value2" ; Property3 = "Value3" } } This string here will SET those properties, and to reference each one later, you would use "$Item.Property1[0]" , you could also reference it by "$Item[0].Property1", whichever way you prefer. If you want to ADD to an existing hashtable, then $Item.Add("AddProperty","AddValue") There are other ways to do that but that seems to be the easiest, at least in my opinion. I could elaborate more? But this comment is getting a little long winded. Hope this helps.
@PabloVillaronga
@PabloVillaronga 4 жыл бұрын
Great Video share, i learn a lot , the easy way ! Wouldn;t be great if you post the link to the powershell you explained to Github or your site,like a blog ? Would be easy to execute and get out exercises !
@ShanesCows
@ShanesCows 4 жыл бұрын
Replies to other comment
@dagma3437
@dagma3437 4 жыл бұрын
Hi Shane. First time using Powershell and I need your advice. I have multiple files in a directory I want to create a script that reads in the directory path and for each file in the directory, I want to check if the content of the files have a non ascii character. If there is a non ascii character I want to then write the file name to a output file. I found this scrpt in stackoverflow $nonASCII = "[^\x00-\x7F]" foreach ($_ in [System.IO.File]::ReadLines($source)){ if ($_ -cmatch $nonASCII){ write-output $_ | out-File $output -append } } Issue I have is how to I pass the directory and the files to $source?
@ShanesCows
@ShanesCows 4 жыл бұрын
Save it to a variable? Not sure.
@IsaiasPerez2023
@IsaiasPerez2023 5 жыл бұрын
What Im confused about is the .ojectpropertyname. For some reason that doesnt work for me? is that a property that can go at the end of any alias or does that need to be defined somewhere?
@NMXLY
@NMXLY 4 жыл бұрын
The ".ojectpropertyname" that is after the "$Object" is what your top Columnes are called in your .csv file.(They must be the exact same.) If you type your $object and then add a "." it should give you a list of things you can choose, here you want that to be what you called the Columnes you need the objects from
@basetau3200
@basetau3200 5 жыл бұрын
Awesome video
@ShanesCows
@ShanesCows 5 жыл бұрын
Thanks
@SourabhShah20
@SourabhShah20 3 жыл бұрын
Hi, want help for an CSV file in which a column contains numbers, need to add a add a comment as "compliant" for the numbers between n to n-3, number below n-4 will be tagged "non-compliant". Suppose if today's latest version is 2111 then the lines with 2111, 2110, 2109, 2108 are compliant & the number below 2107 are non compliant. Please help.
@ShanesCows
@ShanesCows 3 жыл бұрын
I am not sure. Sorry
@ZoomZip
@ZoomZip 4 жыл бұрын
One thing I find myself doing is saying in sync with you "And hereeeeeee's our intro'
@ShanesCows
@ShanesCows 4 жыл бұрын
😍 all these years later I still say it. 🥰
@codyf1
@codyf1 3 жыл бұрын
I reversed engineered the foreach command start of command $b="6","3","99" $n=0 while($n -le $b.count){ $h=$b[$n];$n+=1 #do whatever you want below here but anything to do with $n. Basically don't mess with $n. echo $h } #end of foreach loop #everything below here is necessary to make sure that there are no unnecessary variables left. $n=$null $h=$null End of command Output: 6 3 99 the foreach equivalent of this: $b="6","3","99" foreach($h in $b){ echo $h } I know it takes a whole lot less using the foreach command in this example, but mine has a whole lot more customization. For example you couldn't have possibly gotten this output from a foreach command easily. 6 99 With mine though, you can do this easily. Just change the $n+=1 to $n+=2 and done.
@ShanesCows
@ShanesCows 3 жыл бұрын
Thanks for sharing! 😀
@antoniopinho4673
@antoniopinho4673 6 жыл бұрын
Great Job!!
@ShanesCows
@ShanesCows 6 жыл бұрын
Thanks. Glad it helped you.
@sushmamandala5381
@sushmamandala5381 5 жыл бұрын
when i did any changes in .ps1 file, those changes not working in first time. if I run again then working. please solve my issue. i want first time work how?
@ShanesCows
@ShanesCows 5 жыл бұрын
Not sure. Sorry
@dhamsselvaraj6529
@dhamsselvaraj6529 4 жыл бұрын
Hi Can you help me to solve my below problem using powershell Am having 2 csv file ..eg actual ad expected ... Actual Name actualver Chrome 12.4533. Fire eye 3.4 Expected Name expver Chrome 12.4566 Fire eye 3.4 My expected result is Name expver status actualver Chrome 12.4566 differ 12.4533 Fire eye 3.4 nodiffer 3.4
@ShanesCows
@ShanesCows 4 жыл бұрын
NOt sure. Sorry
@pranavsalunkhe3501
@pranavsalunkhe3501 7 жыл бұрын
Hi Can you please make some video on working with power shell on Windows 10, and terminate process with domain administrator Rights, when the UAC is enabled through GP.
@ShanesCows
@ShanesCows 7 жыл бұрын
Can you give me a more detailed explanation of what you are trying to do or maybe share some sample code you are struggling with? I will try to help.
@pranavsalunkhe3501
@pranavsalunkhe3501 7 жыл бұрын
Shane Young I'm trying to kill the process on Windows 10 machine like winwork.exe. from wmi. Get-wmiobject - computer name $computername -class win32_process -filter "Name='winword.exe'" | foreach-object {$_.Terminate()} But it's giving the return value is 2, means Access Denied. Then I've use the -credential and the administrator account details in user name and password, but no luck. Please help me. One small observation from me that if the script running credentials and the process running credentials are same then it's kill that process.
@ShanesCows
@ShanesCows 7 жыл бұрын
Are you running PowerShell as Administrator? Where you have to right click on the PowerShell icon and select "run as administrator".
@pranavsalunkhe3501
@pranavsalunkhe3501 7 жыл бұрын
We are running that script from Windows 7 machine which is logged in with domain administrator account.
@WaterJugFitness
@WaterJugFitness 7 жыл бұрын
First comment and like! BOOM!
@ShanesCows
@ShanesCows 7 жыл бұрын
Who dis? ;)
@michaelvidal8335
@michaelvidal8335 4 жыл бұрын
So you selected $Dog instead of $Cat. I hope you're happy with that..... ps: from a cat
@ShanesCows
@ShanesCows 4 жыл бұрын
😻
Getting started with the PowerShell If Statement
17:53
Shane Young
Рет қаралды 29 М.
PowerShell ForEach-Object
16:14
Shane Young
Рет қаралды 51 М.
How it feels when u walk through first class
00:52
Adam W
Рет қаралды 24 МЛН
버블티로 부자 구별하는법4
00:11
진영민yeongmin
Рет қаралды 23 МЛН
pumpkins #shorts
00:39
Mr DegrEE
Рет қаралды 124 МЛН
Cool Parenting Gadget Against Mosquitos! 🦟👶 #gen
00:21
TheSoul Music Family
Рет қаралды 32 МЛН
PowerShell 7 Tutorials for Beginners #8 : ForEach (Loops)
19:03
JackedProgrammer
Рет қаралды 8 М.
How To Use The Break Statement Inside the Array forEach Method
11:54
Code Explained
Рет қаралды 1,5 М.
PowerShell String Manipulation
16:54
Shane Young
Рет қаралды 84 М.
PowerShell ForEach and CSV File Tutorial
15:01
Jeff Brown Tech
Рет қаралды 630
MicroNugget: How to Sort and Format PowerShell Output
10:57
CBT Nuggets
Рет қаралды 7 М.
PowerShell Tutorial 8 : ForEach Loops [Beginners]
13:09
JackedProgrammer
Рет қаралды 15 М.
Lambda Calculus - Computerphile
12:40
Computerphile
Рет қаралды 1 МЛН
How it feels when u walk through first class
00:52
Adam W
Рет қаралды 24 МЛН