Code Monkey home page Code Monkey logo

lets-learn-powershell's Introduction

Let's Learn PowerShell

✅ = Lesson Completed

Introduction

The goal of this repository is to give you examples and lessons for learning PowerShell.

Warning

Even though this is common sense, I need to put it in there:

Do not run any code from the internet that you do not understand! Always read the code before running it.

Prerequisites and useful resources

The lessons in here are all based on PowerShell 7, and using VSCode as the editor. You can download them here:

Installing PowerShell

Installing VSCode

PowerShell in VSCode

PowerShell official documentation

PowerShell About Topics: Those articles are great for learning about specific topics in PowerShell which are not necessarily cmdlets or modules.

The basics

✅ Cmdlets

Cmdlets (pronounces 'command lets') are commands in PowerShell used to run actions.

Lesson Code

✅ Variables

Variables are a way to store information so that it can be used later.

Lesson

✅ Strings

Strings deserve their own section because they are usually used for storing data and giving the user feedback. That's why it's important to learn how to form strings.

Lesson

✅ Arrays

Arrays are a collection of values, it allows you to store multiple values in one object. Think of it as a list for your groceries, the piece of paper is the array and each item is a element of the array.

Lesson

✅ Hashtables

Hashtables are a data structure that allows you to store data similar to an array except it is stored in key/value pairs.

Lesson

✅ Loops

Loops are one of the best parts of programming. It allows you to do the same thing over and over again without having to writing the same thing over and over again.

Lesson

✅ Conditionals

When you write code, you often have to handle different scenarios, that's where conditional statements come in.

Lesson

✅ Functions

If you have to perform the same actions over and over again and every time it takes a few lines of code to do it, then functions are your friend!

Lesson

✅ (Optional) Aliases

Aliases are a shortened way to invoke cmdlets and other PowerShell entities. They are not considered best practice and should be avoided as much as possible. They are only in this lesson plan because some people do use them and it can be useful for you to know them. There will be no lesson about them, but feel free to read the documentation about them:

Documentation

A little more advanced

Pipes

Advanced functions

Start-job

Workflows

Azure

Azure CLI

ARM templates

Azure Functions

Azure Automation

lets-learn-powershell's People

Contributors

carolinechiari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

lets-learn-powershell's Issues

Just some comments

Your repo was shared in the Linux channel of our discord (https://aka.ms/psdiscord), I wanted to add a few comments to help in the creation of this project :)

Please feel free to disregard / delete / destroy this. It is intended to help, but you are of course entitled to disagree with my ambition :)

One for the array indexes

You can use $array[1, 4, 9] to access elements of an array. An extension of accessing a range of elements (considering that 1..3 yields an array of 1, 2, 3).

return

Return causes an invoked script block to immediately end. It can also emit, but it is not exclusive and anything else which yields output will also be emitted from a function. It is therefore not accurate to say it specifies explicitly what gets returned as that implies it is all that gets returned.

The function below demonstrates how return can be considered misleading:

function Get-Something {
    $intendedOutput = 1
    'some-other-statement'
    return $intendedOutput
}

This is a common cause of some confusion iin PowerShell for anyone entering from another language. In other languages this would be far more true, in PS it is only exclusive in PS class methods.

Get-Rolls

Consider revising the function to avoid continually resizing a fixed size array. This technique does not scale at all well and can be avoided.

All statements can be assigned in PowerShell, therefore the loop can be assigned. PowerShell will internally build the collection (as an ArrayList), assigning as a final action. The engine is better at this task that anything in PS code.

function Get-Rolls {
    param (
        [int]$NumberOfRolls = 10 # 10 is the default value now
    )

    $roles = for ($i = 0; $i -lt $NumberOfRolls; $i++){
        Get-Random -InputObject @(1..10)
    }
    return $roles
}

Or in the case of a function, output can be immediately sent to the output pipeline. This provides much better support for pipeline commands like Select-Object -First 1.

function Get-Rolls {
    param (
        [int]$NumberOfRolls = 10 # 10 is the default value now
    )

    for ($i = 0; $i -lt $NumberOfRolls; $i++){
        Get-Random -InputObject @(1..10)
    }
}

The example usage below demonstrates that Select-Object stops the command processing:

function Get-Rolls {
    param (
        [int]$NumberOfRolls = 10 # 10 is the default value now
    )

    for ($i = 0; $i -lt $NumberOfRolls; $i++){
        Get-Random -InputObject @(1..10)
        Write-Host 'Looping'
    }
}
Get-Rolls -NumberOfRolls 1000000 | Select-Object -First 2

This solution is inappropriate where clean-up actions are required after a loop, but a good fit in this example.

It is, of course, entirely feasible to create a more advanced collection type, however that is I think beyond the introductory topic you currently have. An example is below just in case:

function Get-Rolls {
    param (
        [int]$NumberOfRolls = 10 # 10 is the default value now
    )

    $rolls = [System.Collections.Generic.List[int]]::new()
    for ($i = 0; $i -lt $NumberOfRolls; $i++){
        $rolls.Add((Get-Random -InputObject @(1..10)))
    }
    return $rolls
}

Despite the List type, PS will enumerate the output when it emits. The output type will therefore still be Object[] as mentioned in some of your other documents.

Get-RollScore

Switch enumerates, you therefore do not need the foreach loop in the example:

function Get-RollScore {
    param(
        [int[]]$Rolls
    )
    $total = 0
    switch ($Rolls)
    {
        1 {$total+=10}
        2 {$total+=10}
        3 {$total+=1}
        4 {$total+=5}
        5 {$total+=9}
        6 {$total+=6}
        7 {$total+=4}
        8 {$total+=8}
        9 {$total+=9}
        10 {$total+=2}
    }
    return $total
}

All the best,

Chris

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.