PowerShell ForEach-Object

  Рет қаралды 51,122

Shane Young

Shane Young

Күн бұрын

ForEach-Object is one of the most commonly used PowerShell cmdlets because it lets you run everything inside the curly braces once for each object you pass to the cmdlet. This video will be your guide to using ForEach-Object successfully. Also covered in the video is import-csv and a simple array. We also remind everyone that aliases are bad and that ForEach-Object and foreach can be two different things.
You can download the PowerShell and CSV file in a zip file from here. sellfy.com/p/uH0Q/
For support, training, or more information about PowerShell check out www.boldzebras.com

Пікірлер: 89
@gregorymendyka
@gregorymendyka Жыл бұрын
Thanks for your time, Shane. Greetings from Scotland.
@ShanesCows
@ShanesCows Жыл бұрын
Thank you! Greetings right back at you. 😎
@Corrado49
@Corrado49 2 жыл бұрын
Thanks for this video, almost five years later continues to be very useful!
@ShanesCows
@ShanesCows 2 жыл бұрын
Great to hear!
@valentinebarbier6156
@valentinebarbier6156 3 жыл бұрын
Thank you for these PowerShell videos! I've really been struggling and afraid of scripting. You made it way easier for me to get into, and i'm actually enjoying working on that! Thanks again :)
@ShanesCows
@ShanesCows 3 жыл бұрын
PowerShell is so much fun. Enjoy. 😎
@CEL2815
@CEL2815 3 жыл бұрын
Hey Shane, thanks so much for making these videos. I've read books on PowerShell, but there's NOTHING like learning visually. Your videos are simple to understand, and follow along and makes it easier for me to WANT to learn PowerShell. Thanks again. If I come across topics I want to learn more about, I will send a comment to see if you can make a video. Have a great day!
@ShanesCows
@ShanesCows 3 жыл бұрын
Glad you like them!
@KSR86
@KSR86 6 жыл бұрын
HI Shane, Your tutorials are very informative and I am learning out of it. Thank you!!!
@ShanesCows
@ShanesCows 6 жыл бұрын
Thanks! :)
@EdResleff
@EdResleff 5 жыл бұрын
You're making me really like PowerShell... again! I got away from using it and then had to jump back in with some intrepidation and a healthy dose of trepidation in my current gig. Am faced with learning a PS script that is overloaded with C++ and C# coding characteristics, but also using some Hungarian notation that has me pulling my hair out... what little I have left. However, your explanations are so calming (and at times a little humorous) that I'm able to sigh and say "This ain't so bad" and I've actually played around with your last ForEach-Object example in this video to get some good stuff out of my script... Yay..! Awesome job on the video!
@ShanesCows
@ShanesCows 5 жыл бұрын
Awesome 😎
@ashmsport1562
@ashmsport1562 6 жыл бұрын
Thank you Shane. Great videos!!!
@stefanspinu8784
@stefanspinu8784 5 жыл бұрын
Man, you're great. Too bad this video has just over 100 likes - it's worth millions. Keep up the good work!
@ShanesCows
@ShanesCows 5 жыл бұрын
Your comment is worth millions to me. Thanks! 😎🤑
@liondk1589
@liondk1589 3 жыл бұрын
These videos are helping me so much for my exams THANK YOU!!!
@ShanesCows
@ShanesCows 3 жыл бұрын
Awesome!
@skfarid9069
@skfarid9069 3 жыл бұрын
Your tutorials are really awesome and very informative .Thanks
@ShanesCows
@ShanesCows 3 жыл бұрын
Glad you like them!
@lauramoney1533
@lauramoney1533 6 жыл бұрын
Thank you for the great tutorial!
@ShanesCows
@ShanesCows 6 жыл бұрын
You are welcome. Thanks for watching and I am always open to new video ideas.
@SCADASupportPh
@SCADASupportPh Жыл бұрын
@shaneyoung is awesome, Shane is cool.... you forgot to mention "Shane is good at educating people". I never get tired of watching these PS script presentation of yours. Thank you Shane for sharing! GOD Bless!
@ShanesCows
@ShanesCows Жыл бұрын
Very kind of you. Have a great weekend!
@aynellrodriguez7348
@aynellrodriguez7348 3 жыл бұрын
Thanks Shane, very useful and easy to understand..
@ShanesCows
@ShanesCows 3 жыл бұрын
Glad to help. 😀
@Aarvens
@Aarvens 5 жыл бұрын
Great instruction, great video. Thanks!
@ShanesCows
@ShanesCows 5 жыл бұрын
You are welcome.
@billyridgeway4074
@billyridgeway4074 6 жыл бұрын
Thanks you, great video.
@talosvalcoran4714
@talosvalcoran4714 4 жыл бұрын
Good video, Shane. Thank you.
@ShanesCows
@ShanesCows 4 жыл бұрын
Happy to help Talos
@allanrabindran5695
@allanrabindran5695 3 жыл бұрын
East or West Shane is the Best ❤️
@ShanesCows
@ShanesCows 3 жыл бұрын
Thanks
@swapneshgodkar4192
@swapneshgodkar4192 4 жыл бұрын
I got you as my first Powershell instructor.. :)
@ShanesCows
@ShanesCows 4 жыл бұрын
Awesome 🤩
@jfreck
@jfreck 5 жыл бұрын
Great another awesome topic I'm gonna be able to use. Hoping to use for remote server functions. Being able to distingush the whole "DEV" group will be handy. Thanms
@ShanesCows
@ShanesCows 5 жыл бұрын
Cool
@Bule2805
@Bule2805 5 жыл бұрын
Hi Shane! Great video as always, I'm learning a lot and having so much fun. Just a quick note: I know this video was made almost two years ago and I'm not sure whether Excel or PowerShell new versions have anything to do with this, but I had a bit of a brainer with the import-csv table format as all data was displaying in strings separated by semicolons, merging the information from columns A, B and C, instead of the nice and neat table you are showing. Got a bit frustrated until I figured out I was missing the -delimiter ";" at the end of line 1 (and 3, subsequently). Not sure if it has happened to anyone else following your vids on 2019, but whatever the case, you are a Rockstar and I thought you might want to know in case the feedback is interesting enough for other people. Keep up the amazing work and thank you again!
@ShanesCows
@ShanesCows 5 жыл бұрын
Awesome. Thanks for sharing.
@jakubberger5399
@jakubberger5399 3 жыл бұрын
For me worked -UseCulture at the end of the line (PowerShell 7.1).
@cool24a
@cool24a 6 жыл бұрын
You have the best intro!
@ShanesCows
@ShanesCows 6 жыл бұрын
Thank you! You don't know how much that means to me. I spend way too much time trying to get the first minute right to get people to watch the whole thing.
@omerfarukerkul1197
@omerfarukerkul1197 5 жыл бұрын
@@ShanesCows Heeeeere is our intro 👉👉
@shaktisingh8752
@shaktisingh8752 5 жыл бұрын
Sir, Really helpful.
@ShanesCows
@ShanesCows 5 жыл бұрын
Thanks
@sithmaster56a
@sithmaster56a 5 жыл бұрын
hi Shane, have you covered error catching in one of your PowerShell videos as I have tried to learn this in the past and I’m having a real hard time getting my head around it. also really appreciate these videos they've shown me a few ways of doings things easier in my scripts. thanks
@ShanesCows
@ShanesCows 5 жыл бұрын
Sorry. I haven’t. It is a topic I have always wanted to tackle and just have not.
@jolenec7159
@jolenec7159 4 жыл бұрын
Thanks, Shane! Is there any way to compare the contents of multiple files (two folders, different versions)? Compare-object seems to only provide the basic info of different file names.
@ShanesCows
@ShanesCows 4 жыл бұрын
I am not sure. 😑 I haven't tried that one before.
@ML-356
@ML-356 6 жыл бұрын
Shane I'm having trouble with powershell, doing the same as above, but I want to send mail message for each line from my CSV. Wondering if you could help?
@ShanesCows
@ShanesCows 6 жыл бұрын
What have you tried? Send-mailmessage should be all you need.
@MohitSharma-if1bg
@MohitSharma-if1bg 4 жыл бұрын
Hey Shane, great video. Really helped me understand the concept of manipulating each row in a CSV and assigning a number value for whatever purpose you need it for. I have a question, how would I manipulate each character from a specific row? For example, in one row I need to evaluate a computer's specs based on a specific number character in the 20th position from the left of the row. If that 20th position character is an i5 (5 being the important piece here) then i do something... how do I read that 20th position of the row. Hope that makes sense....
@ShanesCows
@ShanesCows 4 жыл бұрын
Look for my video on string manipulation
@taseenridwansvlog3965
@taseenridwansvlog3965 2 жыл бұрын
Hey Shane, can you show how to import bookmarks,favorites from chrome to edge using powershell
@elybuendia64
@elybuendia64 4 жыл бұрын
Hi Shayne I need help on comparing 2 csv files and displays their differences...ty
@ShanesCows
@ShanesCows 4 жыл бұрын
Sorry I don't have a video on that.
@SuperKishore20
@SuperKishore20 5 жыл бұрын
Shane is Awesome
@ShanesCows
@ShanesCows 5 жыл бұрын
Thank you
@kipster-ll6po
@kipster-ll6po 6 жыл бұрын
Thanks so much! I am really enjoying this playlist. You are very likable, and easy to learn from. Question about this video: From 13:55 thru 14:00, why didn't "this object is bigger than 1000" get written for every iteration of the loop? Since the variable was NOT reinitialized for the second run, it ended up containing 22,200. Therefore, during that 2nd run, that line of text should have been written to the screen three more times. No?
@ShanesCows
@ShanesCows 6 жыл бұрын
It does. If you watch a few seconds earlier 13:53 I reran the lines so the screen flashes and you can see all three lines are written to the screen again. It happens quickly but you are correct, the screen does get written again as I am making the variable 22,200.
@kipster-ll6po
@kipster-ll6po 6 жыл бұрын
I believe you are talking about the lines that say, "The name of the object is..." However, I was talking about the line that says, "This object is bigger than 1000." Anyway, I eventually figured out my mistake: The if statement is evaluating the value contained in the ObjectPropertyNumber property, NOT the value contained in the $TotalOfNumberColumn variable. Anyway, thanks for the reply -- I know your time is valuable.
@jamesblair110
@jamesblair110 5 жыл бұрын
If you don't get the colored text to work, be sure to save .csv as CSV-UTF8. Regular CSV Comma delimited won't work. I'm using office 365/2016, so may be a quirk.
@ShanesCows
@ShanesCows 5 жыл бұрын
Thanks
@skfarid9069
@skfarid9069 3 жыл бұрын
Hello Shane, I really need a quick help from your side as I need to copy/clone Azure Data Factory entities like Linked services,Datasets, Piplines, Triggers from one environment to another using Powershell only so that it will take value dynamically.The reason is that some of these pipelines may change and it is hard to keep up with changes.So, can it be possible to get all the data from data factory using power shell and then can make a clone of it, because if we update a pipeline, it would be great to copy and paste the logic. It will be of great help if any assistance is provided from your end. Thanks in advance !!! Awaiting for your valuable reply.
@ShanesCows
@ShanesCows 3 жыл бұрын
Sorry not sure.
@mjackstewart
@mjackstewart 4 жыл бұрын
I’ve always been confused by this. If the pipe sends three objects to the next commandlet, why is it necessary to iterate over them in a foreach-object?
@ShanesCows
@ShanesCows 4 жыл бұрын
Because you want to run the cmdlet 3 times, once for each object.
@powerbi-amitsrivstava4451
@powerbi-amitsrivstava4451 3 жыл бұрын
Hi Shane, Nice video :) I need to retrieve Power BI data such as workspace, dataset, dataflows, reports, users etc. Either, this data will come via using one command OR with multiple command yields individual data for every entities with proper foreign key relation ship between them but in either case i dont want to pass any parameter to PowerShell command to retreive result. OR is there any other way out to retrieve such required result. Thanks Amit
@ShanesCows
@ShanesCows 3 жыл бұрын
I am sorry, I don't know.
@atexnik
@atexnik 4 жыл бұрын
If I change the value after -gt to, something else, say, 500 or 5000, then the write-host statement is not being executed. I think, the reason behind it is that the ObjectPropertyNumber is of String type. I wish you added a step to convert it to integer.
@ShanesCows
@ShanesCows 4 жыл бұрын
Sorry it didn’t work for you.
@atexnik
@atexnik 4 жыл бұрын
@@ShanesCows it's not my fault, the object is string. You can't compare string to integer.
@srinivasanrajagopalan3140
@srinivasanrajagopalan3140 3 жыл бұрын
Hi shane..can you make a video for rest api through powershell
@ShanesCows
@ShanesCows 3 жыл бұрын
I haven't done much with that. Sorry.
@userpodcast7350
@userpodcast7350 6 жыл бұрын
Could you program how to program our script if we had a column called Date and we would like to sort our array in Powershell regarding the values in the cells of Date column? Great videos! Like!
@ShanesCows
@ShanesCows 6 жыл бұрын
Do you want to sort the array while you are working with it in PowerShell or save it to a CSV file that is ordered by date?
@userpodcast7350
@userpodcast7350 6 жыл бұрын
It's a ledger and I'd like to have an input csv completly rearranged by the date column and save it to an ouput csv file. I guess it involves parsing every value in the cell date to an object date but then I dont know how to do it. Could you please elaborate the solution! Thank you for answering! Your videos are very explanatory! Best regards.
@sulyluna7563
@sulyluna7563 5 жыл бұрын
Hi Shane, I am having a PS coding mental block where I can't figure out how to change the values of a column in a csv file and then output the updated csv. Essentially, I want to take the column "group owner" where the names are listed in displayname format (i.e., "John Smith") and convert them to their respective SamAccountName (i.e., jsmith) and then export the updated csv that now lists all groupowners in SamAccountName format. I've seen solutions online where the script takes the first and last name and "creates" SamAccountNames but that is not a solution in my case because at our company a user's samaccountname is not necessarily first initial+lastname because there are several people that would end up with the same samaccountname in AD which is not acceptable. My script would need to contain the code "$SamAcctName = Get-ADUser -filter (Name -like "$_.groupowner) | Select Samaccountname" to get the owner's actual samaccountname. My issue is that although my for-eachobject loop is processing each entry of the "groupowner" , it is not saving the person's samaccountname into the variable $SamAcctName. Instead, the output for $SamAcctName is blank. Below is my code. I'm hoping that you can spot what I am doing wrong and provide me with the correct code. Thank you in advance. $csv = Import-Csv C:\Temp\TestCsv.csv $csv | ForEach-Object { # sending current value of $_.groupowner to host screen as a control Write-Host "The group owner is:" $_.groupowner # grabbing this group owner's Samaccountname from AD and assigning that value to the variable $SamAcctName $SamAcctName = Get-ADUser -filter 'Name -like "$_.groupowner"' | Select SamAccountName # Here is my attempt at replacing the display name of the group owner with his Samaccountname (username) # by assigning the $_.groupowner item the value contained in $SamAcctName $_.groupowner = $SamAcctName Write-Host "This group owner has samaccountname:" $SamAcctName } Export-csv C:\temp\TestCsv.csv -NoTypeInformation
@ShanesCows
@ShanesCows 5 жыл бұрын
Not sure. Sorry
@sulyluna7563
@sulyluna7563 5 жыл бұрын
No worries, I spotted my error and fixed it. Thank you for looking into it though. Enjoy your labor day weekend
@pa1089
@pa1089 4 жыл бұрын
Hello Shane, Thank you for the videos. I am working on a New user onboarding script and stuck at adding users to the security as well as distribution groups based on location and department. Basically i have a CSV with Location,FirstName,LastName,Department and using IF statements to move users to the location based on the CSV file and able to create users. I am using the same Logic using the IF statement to add users to the security group based on location and department but seems the script only works just one user and remaining untouched. Below is the code for your reference: #Adds user into Department specific security groups If ($Dept -eq 'IT') { Add-ADGroupMember -Identity "IT Security Group" -Members $SAMAccountName } ElseIf ($Dept -eq 'Accounts') { Add-ADGroupMember -Identity "Accounts Security Group" -Members $SAMAccountName } #Adds user into Location specific security groups If ($Location -eq 'Location') { Add-ADGroupMember -Identity "Location Distribution Group" -Members $SAMAccountName } ElseIf ($Location -eq 'Location 2') { Add-ADGroupMember -Identity "Location2 Distribution Group" -Members $SAMAccountName } Can you please advise what am i doing wrong here as i have serached online for long hours and could not make this to work. Appreciate your help on this.
@ShanesCows
@ShanesCows 4 жыл бұрын
Not sure. Sorry
@dabeebinc6508
@dabeebinc6508 4 жыл бұрын
I have a task I need to do can you help me
@ShanesCows
@ShanesCows 4 жыл бұрын
Sorry I am behind on comments. Hope you got it figured out.
@peex1986
@peex1986 2 жыл бұрын
Hi There, good effort but in the first 6' , I don't see anything clearly ....Plz clear your camera. Regards PE
@ShanesCows
@ShanesCows 2 жыл бұрын
Hey, sorry you are having troubles. Must be your internet or KZbin settings. I double checked and the video shows in hi definition for me.
@RyanHeitz
@RyanHeitz 2 жыл бұрын
Thanks for another helpful video, I just subscribed! Would you consider making a video about PS functions like this one kzbin.info/www/bejne/eJXJh6algrutbsk but without so much jargony, computer sciencey language? I really need learn what is being taught in that lesson but it's a bit advanced, assumes some knowledge, and is targeting PhDs in computer science. :-)
How to use PowerShell foreach
14:25
Shane Young
Рет қаралды 41 М.
PowerShell String Manipulation
16:54
Shane Young
Рет қаралды 83 М.
Son ❤️ #shorts by Leisi Show
00:41
Leisi Show
Рет қаралды 7 МЛН
Sigma Kid Hair #funny #sigma #comedy
00:33
CRAZY GREAPA
Рет қаралды 40 МЛН
PowerShell Master Class - Data and Objects
46:10
John Savill's Technical Training
Рет қаралды 25 М.
Copy, Move, Delete files with PowerShell
17:01
Shane Young
Рет қаралды 173 М.
PowerShell Arrays Introduction
19:20
Shane Young
Рет қаралды 53 М.
Learn to securely use Passwords with PowerShell
18:13
Shane Young
Рет қаралды 72 М.
PowerShell Quick Tips : Test-Path
16:22
JackedProgrammer
Рет қаралды 1,3 М.
Look, this is the 97th generation of the phone?
0:13
Edcers
Рет қаралды 8 МЛН
Сколько реально стоит ПК Величайшего?
0:37
Частая ошибка геймеров? 😐 Dareu A710X
1:00
Вэйми
Рет қаралды 5 МЛН