PowerShell Basics and Scripting - potatoscript/windows-os GitHub Wiki

What is PowerShell? πŸ€–

PowerShell is a special tool on your computer that lets you use commands to control your computer. Unlike the Command Prompt, PowerShell is much more powerful! It's like a superhero version of the Command Prompt and can help you automate tasks, control programs, and manage your files!


Step 1: Opening PowerShell πŸ–±οΈ

How to Do It:

  1. Click on the Start Menu (the Windows icon).
  2. Type PowerShell in the search bar.
  3. Click on Windows PowerShell when it appears.

You’ll now see a blue window where you can type your commands! ⚑

Icon:

πŸ–±οΈ The Start Menu with the PowerShell search.


Step 2: PowerShell Commands πŸ“

In PowerShell, we use commands to tell the computer what to do. Commands are like instructions that PowerShell understands. Here are a few basics:

1. Get-Help (Ask for Help) πŸ†˜

PowerShell has a lot of commands, so if you don’t know what a command does, just ask!

How to Do It:

  1. Type Get-Help CommandName (replace "CommandName" with the name of the command you want help with).
    • Example: Get-Help Get-Process will tell you all about the Get-Process command.
  2. Press Enter.

Icon:

❓ A question mark to represent asking for help.


Step 3: Basic Command: Get-Process (List Running Programs) πŸ–₯️

The Get-Process command shows you all the programs that are currently running on your computer.

How to Do It:

  1. Type Get-Process in PowerShell.
  2. Press Enter.
  3. You’ll see a list of all the programs running on your computer.

Icon:

πŸ’» A computer with programs to represent running programs.


Step 4: Working with Files πŸ“‚

In PowerShell, you can work with files just like in the Command Prompt, but with more power! You can create, delete, and move files and folders easily.

1. Create a New File: New-Item

You can create a new file using the New-Item command!

How to Do It:

  1. To create a text file, type New-Item -Path C:\Users\YourName\Desktop -Name "MyNewFile.txt" -ItemType "File".
  2. Press Enter.

This will create a new text file on your desktop.

Icon:

πŸ“ A new file icon to represent creating a file.

2. Delete a File: Remove-Item

To delete a file, use the Remove-Item command.

How to Do It:

  1. To delete a file, type Remove-Item "C:\Users\YourName\Desktop\MyNewFile.txt".
  2. Press Enter.

Icon:

πŸ—‘οΈ A trash bin icon to represent deleting a file.


Step 5: Variables πŸ”‘

In PowerShell, you can create variables to store information like numbers, text, or even lists of items. A variable is like a little box where you can keep stuff!

How to Do It:

  1. Type $myName = "Lucy" to create a variable called $myName and give it the value Lucy.
  2. Press Enter.
  3. Now, type $myName to see the value stored in the variable.

Icon:

πŸ“¦ A box to represent storing information.


Step 6: PowerShell Scripting ✍️

You can combine several commands into one script. A script is like a recipe that tells PowerShell what to do, step by step!

How to Create a Simple Script:

  1. Open Notepad.
  2. Type these commands:
    $myName = "Lucy"
    Write-Output "Hello, $myName!"
    
  3. Save the file as HelloScript.ps1 (make sure the file ends with .ps1).
  4. Open PowerShell, go to the folder where your script is, and type .\HelloScript.ps1.
  5. Press Enter.

Now, PowerShell will say "Hello, Lucy!".

Icon:

πŸ“ A paper and pen icon to represent scripting.


Step 7: Loops and If Statements πŸ”„

Loops and If statements let you repeat actions or make decisions in your scripts. They're like telling PowerShell to keep doing something until it's done, or only do something if certain conditions are met!

1. For Loop (Repeat Actions) πŸ”

If you want PowerShell to repeat something many times, you can use a For Loop.

Example:

For ($i = 1; $i -le 5; $i++) {
    Write-Output "This is loop number $i"
}

This will print the message 5 times.

Icon:

πŸ” A loop icon to represent repeating actions.

2. If Statement (Make Decisions) ❓

You can use an If statement to check if something is true or false, and do something based on that.

Example:

$myAge = 10
If ($myAge -gt 5) {
    Write-Output "You are older than 5!"
}

This will say "You are older than 5!" because 10 is greater than 5.

Icon:

πŸ€” A decision icon to represent making choices.


Step 8: Running Scripts Safely πŸ”’

Sometimes, PowerShell will stop you from running scripts because it’s worried they might be harmful. But you can change the settings to allow scripts to run.

How to Do It:

  1. Open PowerShell as Administrator (Right-click on PowerShell and choose Run as Administrator).
  2. Type Set-ExecutionPolicy RemoteSigned.
  3. Press Enter.

This allows PowerShell to run your own scripts safely.

Icon:

πŸ” A lock icon to represent security.


Step 9: Getting More Help πŸ†˜

If you ever get stuck or want to learn more commands, PowerShell has a built-in help system.

How to Do It:

  1. Type Get-Help in PowerShell.
  2. Press Enter.
  3. You’ll see a list of commands and how they work!

Icon:

❓ A question mark to represent asking for help.