PowerShell Arrays Introduction

Ғылым және технология

In this video we talk about PowerShell Arrays and how I use them with new-object to create CSV files of data that I can work with in a familiar way. Also covered are hashtables and few other tricks.
For support, training, or more information about PowerShell check out www.boldzebras.com
Here is the actual PowerShell from the video.
$Array = @()
$Processes = Get-Process
Foreach($Proc in $Processes)
{
If($Proc.WS/1mb -gt 100)
{
$Array += New-Object psobject -Property @{'ProcessName' = $Proc.name; 'WorkingSet' = $Proc.ws}
}
}
$Array | select 'ProcessName', 'WorkingSet' | Export-Csv .\file2.csv -NoTypeInformation
$CSVImport = @()
$CSVImport = Import-Csv .\file2.csv
ForEach($dog in $CSVImport){Write-host "Process Name:" $dog.processname " Working Set:" $dog.workingset}
#Just want to look at it in a prettier way?
$CSVImport | Format-Table -AutoSize
#Other tricks to keep in your pocket
$CSVImport[1].ProcessName

Пікірлер: 104

  • @Biska_Wolf
    @Biska_Wolf6 жыл бұрын

    Shane, loving the videos on PowerShell! You're a good teacher! I was wondering if you could do a more advanced video on Arrays? I'm stuck, I've built on Array of Server Names from a XenApp command, then I have a ForEach statment running against that Array of Server Names, but I'd like to know how to Add additional information/psobjects into this existing array (the new information gathered from the ForEach statement), is this possible?

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    So here is what I would try first. In the steps where you create the object to put in the array add the properties you want to add later with dummy data. Then change the values later which should be straightforward. Try that?

  • @Biska_Wolf

    @Biska_Wolf

    6 жыл бұрын

    Woah thanks for the fast reply! Just tried that, and it works! Thanks PowerShell master! :D

  • @allanrabindran5695
    @allanrabindran56953 жыл бұрын

    I bet you can't find such a friendly trainer anywhere ever.. Shane is all smiles and all brains 👍

  • @ShanesCows

    @ShanesCows

    3 жыл бұрын

    Thanks Allan you are very kind. 🐶

  • @Galileo3k
    @Galileo3k3 жыл бұрын

    I always learn so much as you do your script walks/PowerShell parsing. Thank you Shane!

  • @ShanesCows

    @ShanesCows

    3 жыл бұрын

    Awesome. Glad to help. 😀

  • @bakeruk87
    @bakeruk872 жыл бұрын

    Thanks so much for making these vids. I am forever finding myself involved in migrations/implementations and don't have the powershell skills to do the kind of thing you're demonstrating here. Much appreciated!

  • @freke80
    @freke806 жыл бұрын

    You`re the best teacher I found. Subscribed. Please keep making video`s. :) Thanks!!

  • @Bule2805
    @Bule28055 жыл бұрын

    You rock so much, Shane. I'm having tons of fun while learning PS with your videos. Thanks!!

  • @ShanesCows

    @ShanesCows

    5 жыл бұрын

    Happy to help

  • @jime6517
    @jime65174 жыл бұрын

    I appreciate your high quality work and professionalism. Thank you sir.

  • @ShanesCows

    @ShanesCows

    4 жыл бұрын

    You are welcome 👍

  • @luisolias2595
    @luisolias25955 жыл бұрын

    Your videos are awesome, really well explained! . Many thanks for taking the time and effort to publish them.

  • @ShanesCows

    @ShanesCows

    5 жыл бұрын

    Thanks. 😀

  • @TH3HUTTZ
    @TH3HUTTZ2 жыл бұрын

    Loving your videos, you are a great teacher, i'll be coming back for more, thats for sure

  • @prabur4974
    @prabur49742 жыл бұрын

    Great job man 👌👌 crystal clear and very precise

  • @ShanesCows

    @ShanesCows

    2 жыл бұрын

    Thanks a lot 😊

  • @briancrane7634
    @briancrane76345 жыл бұрын

    Your vids are better than the Udemy courses that I paid $$ for!! Thanks!

  • @ShanesCows

    @ShanesCows

    5 жыл бұрын

    Thank you. 😀

  • @nharzic9248
    @nharzic92483 жыл бұрын

    As always, thank you for another great video. I follow a lot of your more recent PowerApps stuff, which has been brilliant. But I also need to get to grips with PowerShell for managing Teams permission policies and I was getting a bit lost in PowerShell for Teams. The ForEach and ForEach-Object and this Arrays video has been so helpful. I work at a school and we have gone heavily into Teams for remote learning. I need to make sure Teams policies are correctly set for students and staff and so if you ever feel inspired to make a video about Microsoft Teams Policy settings in PowerShell, that would be amazing.

  • @ShanesCows

    @ShanesCows

    3 жыл бұрын

    Noted. 🐶

  • @gautamr3098
    @gautamr30983 жыл бұрын

    I like the content and when some one provides the script example in the video description. Not a PowerShell expert and here to learn like you all. Sorry if i did not understand this correctly, If any one can clarify me this doubt. Instead of creating the array and exporting the data in CSV like the example in the video, The same can be achieved by using a 1 line command without an Array: Get-Process | Where-Object {$_.WorkingSet -GE "10000000"} | Select ProcessName, Workingset | Export-Csv -Path C:\Temp\mydata.csv So i am still not clear on the purpose of using an array from this example.

  • @ShanesCows

    @ShanesCows

    3 жыл бұрын

    Arrays are needed for lots of stuff. I just used this example to help. You are right there are ways to do it otherwise

  • @gautamr3098

    @gautamr3098

    3 жыл бұрын

    @@ShanesCows Thanks for the clarification. After some research I understood this using the below example: $Fruits=@("Apple", "Orange") When you type $Fruits this will return both Apple and Orange as the values in the array and I can further use numbers just to result Apple or Orange.

  • @MichaelToub
    @MichaelToub6 жыл бұрын

    Great Videos on PS !!! Wonderful !!! THANK YOU!!!

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    Cool. Glad it helped.

  • @NancyRileyNuniverse
    @NancyRileyNuniverse6 жыл бұрын

    Awesome, cannot wait to watch and learn, Thanks Shane!

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    Let me know what you think and what else I can do to build from here.

  • @NancyRileyNuniverse

    @NancyRileyNuniverse

    6 жыл бұрын

    You bet!!

  • @999fugazi
    @999fugazi4 жыл бұрын

    Great pace and content - thank you!

  • @ShanesCows

    @ShanesCows

    4 жыл бұрын

    Glad to help. 😀

  • @timmurray5975
    @timmurray59756 жыл бұрын

    Like that you're using ps ise. Having multiple tabs means each step of the show can be named along the top. Pretty cool.

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    Thanks. I should have used the ISE sooner.

  • @lakshyam
    @lakshyam4 жыл бұрын

    This helped me a lot. Thanks Shane.

  • @ShanesCows

    @ShanesCows

    4 жыл бұрын

    Awesome 😎

  • @mrplay96
    @mrplay962 жыл бұрын

    Thank you so much for teaching ❤️

  • @ShanesCows

    @ShanesCows

    2 жыл бұрын

    It's my pleasure

  • @TroyConway
    @TroyConway6 жыл бұрын

    Going to be fun!

  • @chriscase1562
    @chriscase15623 жыл бұрын

    I learned something today thank you.

  • @ShanesCows

    @ShanesCows

    3 жыл бұрын

    Glad to hear it!

  • @lautarob
    @lautarob Жыл бұрын

    I have already watched the first two videos. Thanks!! Excellent way to teach PS. Now in this third video, in minute 5 you use as example (foreach $pp in $processes) and thereafter you list $proc (instead of $pp) and you were able to show the property "ws" for $proc. Should it be for $pp instead of $proc?. Or I missed something there?. Again, thanks. After seen a lot of PS videos, these are among the best.

  • @vaibhavthakare2076
    @vaibhavthakare20764 жыл бұрын

    Great work Shane

  • @ShanesCows

    @ShanesCows

    4 жыл бұрын

    Thanks 😊

  • @simonolofsson7488
    @simonolofsson74885 жыл бұрын

    Cool video! Just a thought from one dev to another: when an array exceeds a certain amount of indices, the method of using the ‘+=‘ operator in an iteration will, if I understand PowerShell array objects correctly, quickly allocate much unnecessary memory due to the fact that PS arrays are immutable, thus this will create a new mirrored array object in memory just with this new index. An iteration of n times, will then result in n number of array objects created in the background with one increment each, where only the last one is complete. correct me if I’m wrong here - this is what I’ve learned about this data structure in PowerShell. To combat this and to have an object that behaves more like a List object in Python, when I iterate and append an array I usually instanciate an arraylist (collections.generic.list[string] or [int] depending on usecase) to have a mutable array which nicely adds with the .Add() method. I see your example as trivial between these two approaches but imagine a scenario with sublists of thousands of indices on a core server where ram == money. How goes your thoughts? / PS isn’t my ‘native tongue’ if you catch my drift.

  • @ShanesCows

    @ShanesCows

    5 жыл бұрын

    I have no idea. This is way smarter of a question than I am qualified for. 😕

  • @simonolofsson7488

    @simonolofsson7488

    5 жыл бұрын

    Thanks anyway, great tutorial :)

  • @phoenixflower1225
    @phoenixflower12252 жыл бұрын

    Thank you!

  • @cuteboy9009
    @cuteboy90095 жыл бұрын

    Great videos Shane ! I've Subscribed. More on hash tables and gui building?

  • @ShanesCows

    @ShanesCows

    5 жыл бұрын

    I have never built a gui. :| There are more videos on tables though.

  • @florianvo7616
    @florianvo76162 жыл бұрын

    I would love if you could upload some troubleshooting session. A recording session where things didnt work out the way you wanted and you had to find your way through powershell to get to the solution without using a browser. The interesting scenarios are when we are struggeling so I think we should see you struggeling. Less staged and prepared

  • @florianvo7616

    @florianvo7616

    2 жыл бұрын

    I'm personally most interested in networking but whatever topic you prefere

  • @florianvo7616

    @florianvo7616

    2 жыл бұрын

    I should add that I dont mean resolving an error message but really finding a solutions for problems. For example watching you dip into a topic thats new to you as well, not just new to the viewer

  • @ShanesCows

    @ShanesCows

    2 жыл бұрын

    Thanks for the idea. :)

  • @vinodrajj
    @vinodrajj4 жыл бұрын

    Video is pretty decent and good. Excellent :) Kudus

  • @ShanesCows

    @ShanesCows

    4 жыл бұрын

    Thanks

  • @AndrewParkinson1
    @AndrewParkinson16 жыл бұрын

    Great Shane, thanks. Big fan. Much prefer the ISE. I've been watching your videos in preparation for migrating my fileshares to sharepoint. Big sticking point was file dates. So I was going to buy your advanced script then I realised i had a problem. My problem is I have thousands of old pdf files. Before I migrate them to Sharepoint I want to run OCR on the them. I have Acrobat 9 Pro and it will perform OCR on the whole lot but it will not only change the modified date but the created date. It effectively creates new files (with same name if you so desire). So I've created the new files in a new folder. I have a csv with the file names and the original dates from the original folder and I can pull that into an array in Powershell (thanks to you) but I cannot work out how to use that to modify the CreationTime property of each of the new files in my new folder. Three days of Googling and trial and error keeps drawing blanks!. My csv columns are filename and filecreationtime. I thought it might be as simple as $MyArrayDates = @() $MyArrayDates = Import-Csv .\NewFileDates.csv ForEach-Object { (Get-Item $_.filename).CreationTime = (Get-date $_.filecreationTime) } No errors but it doesn't "DO" anything either! So obviously it aint that simple! I think the data in my arrray is good but wonder how I should format the filecreationtime in the csv, tried various with no luck. So for each file I want to find the matching name in the array and modify the filecreationtime with the corresponding date and time from the array. Or compare the two directories and copy the dates and times for each file from original folder to new folder. Any pointers gratefully accepted.

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    Andrew - Looking at your code the reason it isn't doing anything is you are not setting anything. You are are just getting. :) I think you are real close. Don't give up. :)

  • @AndrewParkinson1

    @AndrewParkinson1

    6 жыл бұрын

    Thanks for the encouragement Shane! I'll persevere. I haven't quite "got it" yet

  • @AndrewParkinson1

    @AndrewParkinson1

    6 жыл бұрын

    Got it! Basically just added "|" to the end of my second line. Grrr thought it was there. Well it's working on my test environment with 6 files now to see if it will scale. Learnt a lot and I think I'm close. Write-Host "Shane is REALLY COOL!!!" -ForegroundColor Green Many, many thanks. :)

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    Thank you for sharing. Glad you got it. :)

  • @fayasputhukkudi1067
    @fayasputhukkudi1067 Жыл бұрын

    Thanks for the video Shane. Starting to learn powershell with your videos. I have a question, Might be a stupid one, What is the need for creating a new object? Can't we just append the array with properties without needing to create an object?

  • @brettford946
    @brettford9462 жыл бұрын

    Thank you for this! I guess I still don't understand the differences between a list and an array though.

  • @jonkarl123
    @jonkarl1235 жыл бұрын

    thank you this help I stored the hyperlink so when brain goes dead

  • @ShanesCows

    @ShanesCows

    5 жыл бұрын

    😀

  • @limacal
    @limacal4 жыл бұрын

    Great Job Shane! Do you have more video about Arrays?

  • @ShanesCows

    @ShanesCows

    4 жыл бұрын

    I don't think so... sorry Carlos

  • @Rood67
    @Rood676 жыл бұрын

    KEEP USING THE ISE!!!

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    Got it. :)

  • @tfre3927
    @tfre39275 жыл бұрын

    Just put “Clear-Host” at the top of the script to clear the screen each time. So much easier.

  • @ShanesCows

    @ShanesCows

    5 жыл бұрын

    Good call. :)

  • @MrJkillam
    @MrJkillam6 жыл бұрын

    i have an interesting one, i am a beginner to PowerShell, i have a CSV file in which i have server information, service tag, serial number, is it virtual, IP address, MAC Address, etc. i am wondering if i have 2 lines the same but only 1 or 2 things are different, example IP and MAC address, is there a way to get this all on 1 line or variable, or array so i only have just that one line with all the information instead of multiple lines?

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    Long story short Yes. It would just require you reading in the data, doing some ifs and then writing data back out. Not something I can give you a quick answer to but with a lot of elbow grease I think you could do it.

  • @ricsmith6464
    @ricsmith64645 жыл бұрын

    I got really curious how come after importing the CSV you could still get values by the same property names until I looked back at 11:00 and saw they were exported like column names in the CSV file. This shouldn't be a shock to me since I did something similar in Java, but seeing this done in PowerShell so easily just blew my mind.

  • @ShanesCows

    @ShanesCows

    5 жыл бұрын

    Glad you got it. 😀 PowerShell is cool.

  • @GyanSingh-oy6iu
    @GyanSingh-oy6iu5 жыл бұрын

    Could please suggest complete guide tutorials on array , set variables etc..from scratch to end so that I could learn more

  • @ShanesCows

    @ShanesCows

    5 жыл бұрын

    Try this video. kzread.info/dash/bejne/d3t-vNGcd5u9Yqg.html

  • @IamWaliJan
    @IamWaliJan5 жыл бұрын

    Hey Shane, i can get the same result with below. is that ok ? any suggestion. Thanks Get-Process | Where-Object {$_.WS/1mb -gt 100} | select Name, Workingset

  • @ShanesCows

    @ShanesCows

    5 жыл бұрын

    I like it. :)

  • @dgcooper96
    @dgcooper966 жыл бұрын

    Can you add another video dealing with the case where if an array only has one element it is automatically converted to the single element type. That is one of the most annoying issues with powershell arrays. Thanks and great video.

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    Interesting. I will see what I can come up with. Thanks for the suggestion.

  • @dgcooper96

    @dgcooper96

    6 жыл бұрын

    Shane Young great! Thanks!

  • @hp3kguy
    @hp3kguy6 жыл бұрын

    Shane, I'm trying your example, using different variable names and it is not working? When I type "$Array" nothing is shown? cls $Array = @() $Filenames = Get-childitem Foreach($Fname in $Filenames) { IF($Fname.Name -contains("-eng.mp3")) { $Array += New-Object psname -Property @{'File Name' = $Fname.Name} } } Here is an example of the file names in the directroy: 2012-04-1040-donald-l-hallstrom-64k-eng.mp3 Help would be appreciated.

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    Try this for your If instead IF($Fname.Name -like "*-eng.mp3" )

  • @hp3kguy

    @hp3kguy

    6 жыл бұрын

    Thanks Shane, that worked after a few edits to your suggestion. For some reason PS complained because there was a space after the " ) in "*-eng.mp3" ) Once I figured out the error and removed that space it worked. Not sure why the -contains didn't work? Thanks again!

  • @bobdillon7254
    @bobdillon72546 жыл бұрын

    And now, hash tables....

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    I know, I know. :)

  • @jaistealth
    @jaistealth6 жыл бұрын

    Can u tell me the diff between objects,arrays and variables(Where n when to use them) If u have already please share the link Thanks fr great videos

  • @hphan276
    @hphan2766 жыл бұрын

    what does -NotypeInformation do

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    It suppresses writing the Object type to the first line of the CSV. Take it off and give it a try. You will see right away the difference.

  • @khaleel22dec
    @khaleel22dec6 жыл бұрын

    How to use auto filter for a specific text in a column.

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    Can you give me an example?

  • @khaleel22dec

    @khaleel22dec

    6 жыл бұрын

    #search $ySearch = $xl.WorksheetFunction.Match($oSearch,$Rng,0) # gives you the ROW # of the found cell $range3 = $ws1.range("BD$ySearch") [void]$range3.select() $z = $ySearch - 1 Write-Host $z #filter for column $xlFilterValues = 7 #filter for column $FL = @("Mark", "John") $rng=$Worksheet.cells.item(2,38).entirecolumn $rng.select | Out-Null $excel.Selection.AutoFilter(56,$FL,$null) I am trying to auto filter a group of Name in a column in 56 using the search in Excel. the search is happening but it is not fetching up the group name as Autofilter. group of name searched. Kindly advice how to fix the Autofilter tried with $filtertext/$filterValues(Just try) it failed.Thanks in advance

  • @khaleel22dec

    @khaleel22dec

    6 жыл бұрын

    Shane Young any luck

  • @ShanesCows

    @ShanesCows

    6 жыл бұрын

    Nope. I had someone else also look at it and neither of us have used PowerShell that way before to help. Sorry. If you tweet me the question I will retweet it to see if anyone else knows anything. @shanescows

  • @khaleel22dec

    @khaleel22dec

    6 жыл бұрын

    Shane Young thanks a lot I have figured it out

  • @abetuna2707
    @abetuna27072 жыл бұрын

    No offense, you seem to know your thing pretty darn well, but for a beginner like me...i dont get it. Do you have any foundational classes for noobs?

  • @tirelibirefe
    @tirelibirefe3 жыл бұрын

    great courses, very helpful, very teaching but your intro is killing my ears man. Couldn't you have found more disturbing, more annoying intro?

  • @ShanesCows

    @ShanesCows

    3 жыл бұрын

    It gets better. I turn it down quite a bit in later videos. I was new to YT when I made this stuff.

  • @c016smith52
    @c016smith522 жыл бұрын

    Super valuable stuff - this little trick at 5:09 is super handy, I have had a need for this so I know it will be very useful! e.g. foreach($pp in $Processes){Write-Host "Hi"} Thank you!

Келесі