Abraham Zinala
Abraham Zinala

Reputation: 4694

Counting in powershell?

Can someone explain to me whats going on here? Its a piece of code i got from a script we use here at work and i believe that i understand why it counts but, from there im lost. Any generalization on why/how it does so would be greatly appreciated.

Please note, i did search everywhere before asking on here.

    $gc = Get-ChildItem C:\users | Select-Object -ExpandProperty Name
    $ls = @($gc)
    $gcls = $ls.count
    For($i=0; $I -lt $gcls; $i++){
    Write-host "$($i): $($ls[$i])"       
    } 
    $selection = Read-Host "Enter Number"
    $selection = $selection -split " "
    $gc[$selection]

gc is self explanatory.

ls is as well throwing the output into an array

gcls is creating the variable to the list of counted strings

I kinda understand whats going on in the for statement where its setting $i to 0, saying if $i -lt the counted strings in $gcls (which it is due to $i=0), and it is counting the output. Now im still kind of following but, I just don't seem to understand how its outputting the strings the way it is.

Anyone familiar with this?

Upvotes: 0

Views: 190

Answers (1)

Pete
Pete

Reputation: 36

Lee_Dailey also answered this above as a comment.

Inlined comments explaining what each line does and where the count comes from, how the write-host works, etc.

$gc = Get-ChildItem C:\users | Select-Object -ExpandProperty Name #gets all items in c:\users
$ls = @($gc) #this seems redundant to me, but, puts output from get-childitem above into $ls
$gcls = $ls.count #stores a count of items found in get-childitem in $gcls

For($i=0; $I -lt $gcls; $i++){
<#
check out https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_for?view=powershell-7.1

specifically: 
    The For statement (also known as a For loop) is a language construct you can use to create a loop 
    that runs commands in a command block while a specified condition evaluates to $true.

    So this will run the statement in the scriptblock (Write-host) while $i is less than $gcls (the count of items found in get-childitem).
    each time it loops, it willll print $($i): $($ls[$i]) to the console and then increase $i by 1 (the $i++ in the For)

breaking down the print statement:
    
    $($i) - prints the current loop count. The $() is a subexpression operator. It isnt really needed here, but it isnt hurting anything see https://ss64.com/ps/syntax-operators.html

    $($ls[$i]) - we have a subexpression operator again. This time were printing a value in the variable $ls. The [$i] gets an item from the array. We need the $(), otherwise it would print all the contents of $ls rather than just the one item we wanted - try it yourself write-host "$($ls[0])" vs  write-host "$ls[0]"
    $ls[0] would get the first item in the array
    $ls[1] would get the second, so on and so forth. Can see https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-7.1 for more info

#>

Write-host "$($i): $($ls[$i])"       
} 
$selection = Read-Host "Enter Number" #prompts the user for input, expects INT seperated by spaces - 5 10
$selection = $selection -split " " #splits the user input
$gc[$selection] #prints the names using user input above. If the user enters 5, theyd get the 5th item returned by Get-ChildItem. Entering "5 10" would get the 5th and the 10th item. Again, see about_arrays above

Upvotes: 1

Related Questions