output while looping

Welcome Forums General PowerShell Q&A output while looping

Viewing 10 reply threads
  • Author
    Posts
    • #189982
      Participant
      Topics: 1
      Replies: 9
      Points: 35
      Rank: Member

      hello,

      I am a student at the present time doing IT Engineering. This is my first time in this industry. I am coming from a construction background and coming into IT is a great world, but I am as green as I can be. I love this world but is very challenging and I have been looking for mentoring but is soo hard to find. So, I come to you all for assistance.

      At the present time I am doing Powershell and the information they give me is vague and challenging. I just finish Linux which I love and is very similar to Powershell. I get it but what I am having a bit of a challenge understanding how an output is generated.

      for example I have this Loop statement I have to output, but I don't understand how to do it. May anyone assist me please and thnk you: this is the example:

      Do {

      if($I -lt 10)

      {new-item -path C: -name $strprefix$intpad$i -type directory}

      else

      {new-item -path C: -name $strprefix$i -type directory}

      then they tell me to, "set $i to increment by 1" they want me to close the stamen with this }  then to end the loop

      Until ($i -eq intfolders +1)

       

      ok so that is all the loop exercise. how do I make this loop work? this is where I am stuck. please assist me on how to make this work. thank you.

    • #190009
      Participant
      Topics: 1
      Replies: 9
      Points: 35
      Rank: Member

      I solved the issue. I needed to set up my variables first, then on my closing I needed to add the $ on the ($i -eq intfolders +1) syntax to complete the loop. Thank you guys.

    • #190027
      Participant
      Topics: 9
      Replies: 1277
      Points: 1,051
      Helping Hand
      Rank: Community Hero

      A more traditional $i loop is using for, like so:

      for ( $i=0;$i -le 10;$i++ ) {
          New-Item -Path "C:\Scripts\Temp$i.txt" -ItemType File -Force
      }
      
      
          Directory: C:\Scripts
      
      
      Mode                LastWriteTime         Length Name                                                                                                                                                                                                            
      ----                -------------         ------ ----                                                                                                                                                                                                            
      -a----       11/22/2019   4:25 PM              0 Temp0.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp1.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp2.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp3.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp4.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp5.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp6.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp7.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp8.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp9.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp10.txt
      

      Do Until is typically used to wait for something to happen. For instance, type the right thing:

      PS C:\Users\rasim> 
      Do {
          $response = Read-Host 'Please type 1'
      
          if ($response -ne 1) {
              'Umm, I said type 1'
          }
      }
      Until ($response -eq 1)
      
      Please type 1: 4
      Umm, I said type 1
      Please type 1: 3
      Umm, I said type 1
      Please type 1: 2
      Umm, I said type 1
      Please type 1: r
      Umm, I said type 1
      Please type 1: 1
      
      • #190039
        Participant
        Topics: 1
        Replies: 9
        Points: 35
        Rank: Member

        oh wow! thank you Rob for that detail presentation. Now that is what I am talking about. thank you. I need to practice this more. Being new in the field is satisfying as I will learn good habits. I will do my best to practice PowerShell. Greatly appreciated.

        Josue

      • #190063
        Participant
        Topics: 1
        Replies: 9
        Points: 35
        Rank: Member

        hey Rob,

        I tried both your exercises. and came out great. but my question is, the second exercise, why would I need that type of function? is it for something practical or was it for fun to show me how the commands work. Is very neat. it took me a bit to figure out that I needed to input a number in order for it to work as it shows in your example. As well it gives me an error that it can not find "c:\users\rasim>" and asking me to verify the process name and call the cmdlet again. why is that?

        Josue

      • #190861
        Participant
        Topics: 1
        Replies: 9
        Points: 35
        Rank: Member

        G'day Rob,
        so i finally made the loop go on a long time, it has been running for two days and not stopped yet. It has generated 1427540 files. now, question once I stop it, how do I delete all those files?
        How I made it go non-stop is that I replaced the 10 value to 1000 value.
        this is the program:

        $intfolders=1000 (the original value was 10 i changed to 1000)
        $intpad=0
        $i=1

        New-variable -name strprefix -value "testfolder" -option constant
        do {
        if($i -lt 10)
        {new-item -path c: -name $strprefix$interpad$i -type directory}
        else
        {new-item -path c: -name $strprefix$i -type directory}
        $i++
        }
        until ($i -eq $interfolders +1)

        so, how do you delete all these folders? please and thnk you.

    • #190030
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      If you wnat to create 10 folders with increasing numbers in their names you can do easier ...

      1..10 | 
          ForEach-Object{
              New-Item -Path C: -Name $strprefix$intpad$PSItem -ItemType Directory
          }
      • #190042
        Participant
        Topics: 1
        Replies: 9
        Points: 35
        Rank: Member

        so, I tested that syntax and it gave me an error in line 1. did I do something wrong? As well what's with the spaces on every line? is there a reason for the spaces or is it a preference on how the syntax looks when you type? I notice that on every exercise in my text books. there is no explanation about the reasoning of the spaces.

        Thank you very much I will definitely practice all these syntaxes in order to become better.

        Josue

    • #190054
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      You may read the The Unofficial PowerShell Best Practices and Style Guide.

      so, I tested that syntax and it gave me an error in line 1.

      If you get errors you should post them completely (formatted as code as well, please)

      • #190069
        Participant
        Topics: 1
        Replies: 9
        Points: 35
        Rank: Member

        the error that it showed me was:
        at line:1 char:1
        +|1..10 |
        +~

        that was the error it gave me when I did your command. Did I do something wrong?

        Josue

    • #190066
      Participant
      Topics: 9
      Replies: 1277
      Points: 1,051
      Helping Hand
      Rank: Community Hero

      When you first start with basic commands, it won't matter as much, but when you start creating scripts you use code indention to basically made code easier to read and understand the sections of the code, here is article

      http://mrbool.com/importance-of-code-indentation/29079

      Take this example:

      function Test-It {
      param (
      $param1,
      $param2
      )
      begin {
      'Do something'
      }
      process {
      $results = foreach ($item in $param1) {
      "Processing $item"
      }
      }
      end {
      $results
      }
      }
      
      function Test-It2 {
          #Indent for contents in function
          param (
              #Indent for contents in param
              $param1,
              $param2
          )
          begin {
              #Indent for contents of begin
              'Do something'
          }
          process {
              #Indent for contents of process
              $results = foreach ($item in $param1) {
                  #Indent for contents of for loop
                  "Processing $item"
              }
          }
          end {
              #Indent for contents of end
              $results
          }
      }
      

      Which one is easier to read? What is inside the function? What is inside the end block? Imagine there is a hundred lines of code, how much harder is the first function to read? There are some other tricks in Powershell, like carriage returns being ignored to make formatting pretty, but it not functional as much as it is readability:

      #Carriage returns after a pipe
      Get-This | Set-This | Export-That
      
      Get-This |
      Set-This |
      Export-That
      
      #Carriage returns after commas
      $myArray = 'one','two','three'
      
      $myArray = 'one',
                 'two',
                 'three'
      
      • #190072
        Participant
        Topics: 1
        Replies: 9
        Points: 35
        Rank: Member

        awesome. thank you. so would the identation cause issues with output of the syntax or not?

        Josue

    • #190087
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      the error that it showed me was:

      at line:1 char:1

      +|1..10 |

      +~

      that was the error it gave me when I did your command. Did I do something wrong?

      Josue

      That's not the code I postest and that doesn't look like a complete error message. Did you notice that you've got answers from two different persons? Rob showed you another solution than me. Please review the links we posted to get you started.

    • #190867
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      I am a student at the present time doing IT Engineering. This is my first time in this industry. I am coming from a construction background and coming into IT is a great world, but I am as green as I can be. I love this world but is very challenging and I have been looking for mentoring but is soo hard to find.

      To learn Powershell also means to research by yourself, to try, to fail, to try again, to fail again and to try again once more. It is beyond the scope of this or any forum to teach you how to write a script step by step.

      You may do a step back and start to learn the very basics of Powershell from scratch. There are a lot of good book, online tutorials or even this free video course where the inventor of Powershell itself introduces basic Powershell for you Microsoft Channel 9 – Getting Started with Microsoft PowerShell 3.0. Please give yourself a little time for these first steps.

      Of course you can come back any time if you get stuck with some particular piece of code you wrote. If you like to remove some items with Powershell you should read the help for Remove-Item. Please read the complete help including the examples to learn how to use it.

    • #190870
      Participant
      Topics: 1
      Replies: 9
      Points: 35
      Rank: Member

      thank you Olaf. I will definitely go over the information you gave me and be a sponge. I will follow these steps you sent me, thank you very much, greatly appreciated. Tahnk you so very much.

    • #191080
      Participant
      Topics: 1
      Replies: 9
      Points: 35
      Rank: Member

      so, i made this script:
      $intfolders=1000 (the original value was 10 i changed to 1000)
      $intpad=0
      $i=1
      New-variable -name strprefix -value "testfolder" -option constant
      do {
      if($i -lt 10)
      {new-item -path c: -name $strprefix$interpad$i -type directory}
      else
      {new-item -path c: -name $strprefix$i -type directory}
      $i++
      }
      until ($i -eq $interfolders +1)

      with Olaf assistance i now know how to delete these folders, but one more question, how to stop it. By going through what Olaf passed on to me, it was extremely helpful. I am researching how to stop the it but no luck. any suggestions?

    • #191083
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      You already posted that piece of code and it still does not make any sense to me. For example: the first 2 variables ($intfolders and $intpad) – you declare but you never use. Later you use a variable ($interfolders) you've never declared.

      Now your loops: If you want to create a bunch of folders with indexed names you can achieve this easier:

      0 .. 10 |
          ForEach-Object {
              New-Item -Path C:\temp -ItemType Directory -Name ("NewIndexedFolder_{0:00}" -f $_)
          }

      But you created an infinite loop. It will run until someone or something stops it. What is it actually what you want to achieve?

      And please format your code as code. You should read the first pinned post on top of the list of this forum recommending to read it before you post: Read Me Before Posting! You'll be Glad You Did!

      I recommend for you to read as well The Unofficial Powershell Best Practices and Style Guide.

      If you want to know more about loops you can use the Powershell help:

      Get-Help about_For -ShowWindow
      Get-Help about_Foreach -ShowWindow
      Get-Help about_Do -ShowWindow
      Get-Help about_While -ShowWindow 
Viewing 10 reply threads
  • You must be logged in to reply to this topic.