PowerShell Arrays Introduction

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ก.ย. 2024
  • 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_Wolf 6 ปีที่แล้ว +6

    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 ปีที่แล้ว +2

      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 ปีที่แล้ว +3

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

  • @allanrabindran5695
    @allanrabindran5695 3 ปีที่แล้ว +2

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

    • @ShanesCows
      @ShanesCows  3 ปีที่แล้ว +1

      Thanks Allan you are very kind. 🐶

  • @Galileo3k
    @Galileo3k 4 ปีที่แล้ว +1

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

    • @ShanesCows
      @ShanesCows  4 ปีที่แล้ว

      Awesome. Glad to help. 😀

  • @bakeruk87
    @bakeruk87 2 ปีที่แล้ว

    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!

  • @jime6517
    @jime6517 4 ปีที่แล้ว +1

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

    • @ShanesCows
      @ShanesCows  4 ปีที่แล้ว

      You are welcome 👍

  • @prabur4974
    @prabur4974 3 ปีที่แล้ว +1

    Great job man 👌👌 crystal clear and very precise

    • @ShanesCows
      @ShanesCows  3 ปีที่แล้ว

      Thanks a lot 😊

  • @nharzic9248
    @nharzic9248 3 ปีที่แล้ว +1

    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.

  • @timmurray5975
    @timmurray5975 6 ปีที่แล้ว +5

    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.

  • @TH3HUTTZ
    @TH3HUTTZ 2 ปีที่แล้ว

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

  • @freke80
    @freke80 6 ปีที่แล้ว +1

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

  • @briancrane7634
    @briancrane7634 5 ปีที่แล้ว +2

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

  • @Bule2805
    @Bule2805 5 ปีที่แล้ว +1

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

  • @luisolias2595
    @luisolias2595 5 ปีที่แล้ว +2

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

  • @gautamr3098
    @gautamr3098 3 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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.

  • @lautarob
    @lautarob 2 ปีที่แล้ว

    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.

  • @florianvo7616
    @florianvo7616 3 ปีที่แล้ว +1

    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 3 ปีที่แล้ว

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

    • @florianvo7616
      @florianvo7616 3 ปีที่แล้ว

      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  3 ปีที่แล้ว

      Thanks for the idea. :)

  • @mrplay96
    @mrplay96 3 ปีที่แล้ว +1

    Thank you so much for teaching ❤️

    • @ShanesCows
      @ShanesCows  3 ปีที่แล้ว +1

      It's my pleasure

  • @chriscase1562
    @chriscase1562 3 ปีที่แล้ว +1

    I learned something today thank you.

    • @ShanesCows
      @ShanesCows  3 ปีที่แล้ว

      Glad to hear it!

  • @simonolofsson7488
    @simonolofsson7488 5 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

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

    • @simonolofsson7488
      @simonolofsson7488 5 ปีที่แล้ว

      Thanks anyway, great tutorial :)

  • @lakshyam
    @lakshyam 4 ปีที่แล้ว +1

    This helped me a lot. Thanks Shane.

  • @MichaelToub
    @MichaelToub 6 ปีที่แล้ว +3

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

    • @ShanesCows
      @ShanesCows  6 ปีที่แล้ว

      Cool. Glad it helped.

  • @999fugazi
    @999fugazi 4 ปีที่แล้ว +1

    Great pace and content - thank you!

    • @ShanesCows
      @ShanesCows  4 ปีที่แล้ว

      Glad to help. 😀

  • @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?

  • @NancyRileyNuniverse
    @NancyRileyNuniverse 6 ปีที่แล้ว +1

    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!!

  • @vaibhavthakare2076
    @vaibhavthakare2076 4 ปีที่แล้ว +1

    Great work Shane

  • @limacal
    @limacal 4 ปีที่แล้ว +1

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

    • @ShanesCows
      @ShanesCows  4 ปีที่แล้ว

      I don't think so... sorry Carlos

  • @vinodrajj
    @vinodrajj 4 ปีที่แล้ว +1

    Video is pretty decent and good. Excellent :) Kudus

  • @TroyConway
    @TroyConway 6 ปีที่แล้ว +1

    Going to be fun!

  • @jonkarl123
    @jonkarl123 6 ปีที่แล้ว +1

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

  • @ricsmith6464
    @ricsmith6464 5 ปีที่แล้ว +1

    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-oy6iu 5 ปีที่แล้ว +1

    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. th-cam.com/video/EGJZocG8W2E/w-d-xo.html

  • @AndrewParkinson1
    @AndrewParkinson1 6 ปีที่แล้ว

    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 ปีที่แล้ว +1

      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. :)

  • @cuteboy9009
    @cuteboy9009 5 ปีที่แล้ว

    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.

  • @Rood67
    @Rood67 6 ปีที่แล้ว +3

    KEEP USING THE ISE!!!

  • @brettford946
    @brettford946 2 ปีที่แล้ว

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

  • @phoenixflower1225
    @phoenixflower1225 2 ปีที่แล้ว

    Thank you!

  • @tfre3927
    @tfre3927 5 ปีที่แล้ว +1

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

  • @IamWaliJan
    @IamWaliJan 5 ปีที่แล้ว +1

    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

  • @MrJkillam
    @MrJkillam 6 ปีที่แล้ว +1

    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.

  • @hp3kguy
    @hp3kguy 6 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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!

  • @dgcooper96
    @dgcooper96 6 ปีที่แล้ว

    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!

  • @abetuna2707
    @abetuna2707 2 ปีที่แล้ว

    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?

  • @bobdillon7254
    @bobdillon7254 6 ปีที่แล้ว +4

    And now, hash tables....

    • @ShanesCows
      @ShanesCows  6 ปีที่แล้ว +1

      I know, I know. :)

  • @hphan276
    @hphan276 6 ปีที่แล้ว

    what does -NotypeInformation do

    • @ShanesCows
      @ShanesCows  6 ปีที่แล้ว +1

      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.

  • @jaistealth
    @jaistealth 6 ปีที่แล้ว

    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

  • @khaleel22dec
    @khaleel22dec 6 ปีที่แล้ว

    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 ปีที่แล้ว +1

      Shane Young thanks a lot I have figured it out

  • @tirelibirefe
    @tirelibirefe 3 ปีที่แล้ว +1

    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
    @c016smith52 2 ปีที่แล้ว

    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!