Working with your OS

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is the function of a terminal emulator?

  • Directly interfaces with the operating system kernel.
  • Manages hardware resources.
  • Combines the console and terminal functions. (correct)
  • Transforms commands into machine code.

PowerShell cmdlets can be run on any operating system.

False (B)

What is the primary role of the Shell in relation to the CLI?

The Shell controls the prompt.

What is the purpose of the 'Get-Process' cmdlet in PowerShell?

<p>To list all the processes running on the computer. (B)</p> Signup and view all the answers

The Measure cmdlet provides ______ about collected objects.

<p>quantifiable information</p> Signup and view all the answers

In the context of PowerShell, what does the term 'properties' refer to?

<p>Smaller parts of an object, where each part has its own characteristics or attributes (A)</p> Signup and view all the answers

In PowerShell, piping allows you to send text to another command, but not objects.

<p>False (B)</p> Signup and view all the answers

What is the purpose of using the | Format-List command in PowerShell?

<p>Lists the properties of an object.</p> Signup and view all the answers

In PowerShell, $_ is used for what purpose?

<p>To represent the current item in a pipeline. (A)</p> Signup and view all the answers

Using the Where parameter in a PowerShell command ______ the response to only those objects that match specific command restrictions.

<p>limits</p> Signup and view all the answers

In PowerShell, -eq means 'not equal to'.

<p>False (B)</p> Signup and view all the answers

Given the PowerShell command $ processes = Get-Process | Where { $_.CPU -gt 20 }, what does the -gt operator signify?

<p>Greater than (D)</p> Signup and view all the answers

What is the primary function of the ForEach cmdlet in PowerShell?

<p>Runs a script block for each item in a collection.</p> Signup and view all the answers

When used with the cmdlet ForEach, the symbol $_ refers to the ______ .

<p>indicated item</p> Signup and view all the answers

The command Set-Location is used to create new directories in PowerShell.

<p>False (B)</p> Signup and view all the answers

What purpose do aliases serve in PowerShell?

<p>To create shortened names for frequently used cmdlets. (D)</p> Signup and view all the answers

In relation to Get-ChildItem, what is the alias ls commonly used for?

<p>Get the child parts of wherever we are at present.</p> Signup and view all the answers

In Windows PowerShell, hard disks are not recognized as drives.

<p>False (B)</p> Signup and view all the answers

Match the following PowerShell drives with their corresponding description:

<p>C: = File system for the C drive HKCU: = Registry drive for the current user Env: = Environment variables Alias: = Aliases defined in the current session</p> Signup and view all the answers

Which term best defines a PowerShell cmdlet?

<p>A lightweight command used to perform specific tasks. (C)</p> Signup and view all the answers

New-Item, a PowerShell cmdlet, is used for creating ______ and setting its value .

<p>new items</p> Signup and view all the answers

Parameters enclosed in square brackets in PowerShell are mandatory.

<p>False (B)</p> Signup and view all the answers

To display the commands that match 'process', which command do you run?

<p>Get-Command <em>process</em></p> Signup and view all the answers

What type of internal structure is used for PowerShell?

<p>Modules (A)</p> Signup and view all the answers

To continually have the computer go back to redo the Get processes, PowerShell provides ______.

<p>variables</p> Signup and view all the answers

The correct syntax to assign the number 123 to a variable $t is $t = '123'.

<p>True (A)</p> Signup and view all the answers

What is a ScriptProperty in PowerShell?

<p>A property that runs a script whenever you try to run or change whatever it is. (A)</p> Signup and view all the answers

Match the type to its definition:

<p>NoteProperty = Allows for adding properties to an item Process = The properties and values for a process ScriptProperty = Runs a script whenever you try to run or change whatever it is.</p> Signup and view all the answers

An object has a type. What does this type define?

<p>The members of an object (C)</p> Signup and view all the answers

The cmdlet Get-Process delivers processes, but is not an object.

<p>False (B)</p> Signup and view all the answers

What information does GetType() provide about an object?

<p>It returns the type of an object.</p> Signup and view all the answers

______ are for PowerShell-specific functions.

<p>Commands</p> Signup and view all the answers

Methods are exclusive to .NET things.

<p>True (A)</p> Signup and view all the answers

When would you use a command insteqad of a method?

<p>If you want to delete an object. (A)</p> Signup and view all the answers

Match the term with what it does.

<p>Properties = Look at what's in it, change what's in it. Get-Member = Gets the properties Length = Gets a property of the string</p> Signup and view all the answers

Which of the following is a number?

<p><code>[int]${t}</code> (A)</p> Signup and view all the answers

A string is not an array.

<p>False (B)</p> Signup and view all the answers

If you have a file with the extension ps1, how do you denote it?

<p>./script.ps1</p> Signup and view all the answers

Flashcards

Console

A hardware piece that ran software called the terminal to communicate with the mainframe.

Terminal Emulator

Combines the Console and terminal functions. Its basically a command window.

CLI

Provides the command line interface (CLI) environment to the user. Accepts commands and passes them to a shell.

Shell

Controls the prompt, different shells have different capabilities and commands.

Signup and view all the flashcards

PowerShell

Based on the .Net Framework, incorporates technologies like WMI, is both a shell and a scripting language.

Signup and view all the flashcards

Properties

The parts smaller than a file or folder.

Signup and view all the flashcards

Pipe (|)

A way to give the object over to another command.

Signup and view all the flashcards

Get-Process

Lists processes running on your computer.

Signup and view all the flashcards

Where

Limits the response to objects that match the restrictions placed on the command.

Signup and view all the flashcards

$_

Refers to the indicated item (process in this case).

Signup and view all the flashcards

Echo Command

Allows us to print a message.

Signup and view all the flashcards

Set-Location

Changes the current directory.

Signup and view all the flashcards

Aliases

An alias is where is an alias for the full command: where-Object.

Signup and view all the flashcards

PS Drives

PowerShell organizes everything into Drives

Signup and view all the flashcards

New-Item

Create folders

Signup and view all the flashcards

Parameters

Give more details to commands

Signup and view all the flashcards

Where

Limits the response to objects that match the restrictions placed on the command

Signup and view all the flashcards

PowerShell Modules

Specific libraries made to extend PowerShell

Signup and view all the flashcards

.NET Libraries

Millions of reusable code for .Net projects.

Signup and view all the flashcards

CLR

Object System. Makes code run

Signup and view all the flashcards

GetType()

Returns the type of an object.

Signup and view all the flashcards

Method

Designed specifically for .NET things

Signup and view all the flashcards

Commands

Designed specifically for PowerShell.

Signup and view all the flashcards

Void

this method does not return anything (void).

Signup and view all the flashcards

Integer numbers

byte, short, int, long

Signup and view all the flashcards

Decimal numbers

Float, Double

Signup and view all the flashcards

Dot (.)

Tells PowerShell that we want to access one of the deeper properties on the object.

Signup and view all the flashcards

Script Property

A property that runs a little script whenever you try to run or change whatever it is.

Signup and view all the flashcards

Note Property

The regular properties are now set in stone. The way to add something here is through the use of a NoteProperty.

Signup and view all the flashcards

types

An Object always has a type attached to it. The type describes the members an object will have.

Signup and view all the flashcards

Study Notes

Working with Your OS

  • Console started as hardware like a typewriter connecting to a mainframe
  • The console ran terminal software, communicating with the mainframe
  • A terminal emulator combines the console and terminal functions
  • The terminal, provides the CLI environment, accepting commands and passing them to a shell
  • PowerShell has cmdlets

Shell Prompts

  • The shell controls the prompt
  • PS C:\Users\Miles McDonald> indicates PowerShell
  • C:\Users\Miles McDonald> indicates cmd
  • The prompt can be changed and is followed by the command line

VS Code Intro

  • Create a folder structure, for example a folder named ‘OS’ with an internal folder named 'Week 9 Scripts'
  • Open VS Code, and open the created folder
  • Create new scripts with the icon or by pressing Ctrl+N
  • Select PowerShell 7 as the language by clicking the bottom right-hand side corner
  • You can get auto-complete by typing: Get-Process
  • Right-click and choose ‘Run Selection’ will execute the current line

Displaying and Stopping Processes

  • Running Get-Process will display the processes on your device in the terminal window
  • Running Get-Process -Name Notepad will display Notepad details; if not running, it gives a red line
  • Use Stop-Process -Name Notepad to close Notepad
  • Use Control + ~ to Open/close the terminal window in VS Code
  • Press F1 to change the colour theme etc

PowerShell Overview

  • PowerShell is based on the .Net Framework and incorporates technologies like WMI
  • PowerShell is both a shell and a scripting language
  • Learning PowerShell is essential for network administrators
  • Open PowerShell 7 as administrator and type Get-Help
  • Press 'Return'

PowerShell Scripts

  • PowerShell is built upon objects
  • Important commands include those to filter, sort, and select objects
  • Uses the '$_.', syntax familiar with the environment

PowerShell 7 vs. CLI

  • CLI (bash or Command Prompt) commands return text
  • Using PowerShell CLI commands you can pipe text to another command

Objects

  • PowerShell commands return objects
  • Objects have properties like name, path, and extension

PowerShell and the 'ls' Command

  • Run 'ls' in PowerShell to see results similar to 'bash'
  • 'ls' produces a table showing objects in a text format
  • Use a pipe (|) to pass the object over for the command Format-List
  • Is was taken by PowerShell and formatted via pipe internally as: Is | Format-Table

Get-Process Command

  • It lists all the processes running
  • Get-Process | Format-List shows the same processes in a different format

Measure Cmdlet

  • Measure is short for Measure-Object which tells you the number of objects you get back
  • Example: ls | Measure

Important Commands

  • Where command limits responses to objects matching the command's restrictions

The $_ Syntax

  • 'Where' can run conditions on every item
  • $_ refers to the indicated item, telling the computer to fill in the blank
  • Dot (.) tells PowerShell to access a specific property on the object

ForEach Cmdlet

  • ForEach is used in two different ways
  • Ls provides the three objects, which are then given to ForEach
  • ForEach runs the code specified for each of the files found

Basic Exercise

  • To get processes with an ID above 4000 and calculate the average CPU time, use: Get-Process | where { $.ID -gt 4000} | Sort {$.CPU} | ForEach {$_.CPU} | Measure -Average
  • Use Set-Location to change the current directory

Aliases

  • Aliases are shortcuts for commands
  • example, where is an alias for where-object
  • Ls is an alias for Get-ChildItem, listing child parts of the current location

PS Drives

  • PowerShell organizes everything into drives
  • Windows hard disks and devices show up as drives
  • Navigate entry to the registry drive using cd HKCU:
  • Run find a list Is to show the listing of all the registry entries

Cmdlets

  • Cmdlets are PowerShell commands which are objects, using a verb-noun format

New-Item Cmdlet

  • Creates new items and sets their value, depending on the location
  • Use get-help cmdlet to get information about the New-Item cmdlet

Creating a New File

  • Example: New-Item -Path -Name "tcp.txt" -ItemType "file" -Value "transmission control protocol"
  • It creates files with the date and length

Content Cmdlets

  • Add-Content cmdlet adds to text files
  • Get-Content cmdlet gets the content of text files

Discovering Parameters

  • Parameters provide details for commands
  • The Syntax is: Get-Help (followed by) ‘some command'
  • Optional parameters reside within square brackets

Standard and Non-Standard Filtering

  • For standard filtering: Is | where { $_.Extension -eq '.txt'}
  • Using non-standard filtering is: Ls *.txt

Discovering Cmdlets

  • Cmdlet names are logical, following a verb-noun structure
  • Complete using tab: Get-Network(tab here)
  • This will complete for you if it is right: Get-NetworkSwitchEthernetPort

Command Discovery

  • To list potential commands run: Get-Command
  • To search commands using wildcards (), run: Get-Command | where {$_.Name -like ‘Get-'}

PowerShell Modules

  • PowerShell's internal structure includes modules, libraries, .NET Framework, and CLR

Variables and Operators

  • Use variables to perform function results once to reuse them

Creating Variables

  • Create variables using $var
  • Create an inquiry for Get-Process for CPU usage over 20: $heavyProcesses
  • Set variable equal to the return from our Get-Process enquiry

Variable Reuse

  • This variable now contains the entire table identified we can reuse this information

ForEach and Variables

  • $_ is a variable that allows pulling each (CPU in this case) in turn within ForEach

Turning Text into Numbers

  • If $t = "123", it is text
  • By running [int] $t it becomes an integer

Further Conversion

  • You can convert variables like $students with mathsScore from text to numbers using $students | ForEach {$.mathsScore = [int]$.mathsScore }

Expanded Note Property

  • An object is composed of properties
  • A ScriptProperty runs a script
  • A NoteProperty allows adding after a process is created

PowerShell Script and Columns

  • Example of adding a column to a CSV file called ‘students.csv'
    • Create a CSV file ‘students.csv' with 3 columns, Name, Maths and English.
    • Load the CSV and change text to numbers
    • Add a 'sum' column to all objects in the file
    • Fill in all those properties
    • Save the CSV file

NoTypeInformation

  • Without -NoTypeInformation a resultant CSV will have PSCustom type info
  • -NoTypeInformation instructs the code to not include the type
  • Therefore the data returned to a csv is usable in outside tools

Object Typing

  • An Object always has a type attached to it which describes its members, for examples:
  • Get-Process delivers processes
  • Each process here is an object and each object has same property labels
  • Types come from the central areas of the system

Variable data types

  • CmdletInfo type is from PowerShell Core
  • Process info type is from .NET library

Object Types

  • Object types are:
    • Property
    • NoteProperty
    • Method

Common Methods

  • GetType() is a common method which returns the type of an object

Methods vs. Commands

  • Command is designed specifically for PowerShell
  • Method is designed for all .NET things

Commands and Parameters

  • bool WaitForExit(int milliseconds); method which returns a bool type of object
  • The parameter is an integer and is named 'milliseconds'
  • void M(string a, int b); has two parameters, separated by a comma

C# and PowerShell

  • Most of .NET and PowerShell is written in C#
  • Example Code: string A {get; set;}, the code shows how to look what is in it, and change what's in it

Get-Member and Objects

  • Run Get-Member gives one object and will give a clear list of all the members on an object
  • Gives a list of all the members on the object if one object is provided, otherwise lists members on the first object.

Member Information

  • In get-member, we retrieves the Name, MemberType and the Definitions
  • The list is cut off but further down are the methods and Properties listed too.
  • Methods can have different variants, example: overloads. moveTO()
  • PS C:\Users\Miles McDonald> ls -Directory; Lists directories in current path

Primitive Number Datatypes

  • byte (Byte)
  • sbyte (SByte)
  • short (Int16)
  • ushort (UInt16)
  • int (Int32)
  • uint (UInt32)
  • long (Int64)
  • ulong (UInt64)

Primitive Decimal Datatypes

  • float (Single) - 32 bit
  • double (Double) - 64 bit
    • Note that Doubles are more precise (because they are bigger)

Boolean Datatype

  • isPrimitive Returns: True

String Datatype

  • isPrimitive Returns 'False'

One property on the string.

  • Int Length {get;}; The length is one property of all strings

Single Quotes vs. Double Quotes

  • To break out of the regular usage for quotes, use single quotes
  • For example:
    • $age = 50'I am $age years old'Will return I am $age years old “I am $($age + 5) years old"Will return I am 55 years old

Concatenation

  • $str = 'abc'$str + ''; returns abc
  • $str += ''; Returns abc. $str has changed its value to abc.

Arrays

  • $myArray = @(), is an empty array, Notice the naming is the same as a variable
  • $myArray.count, this will return 0 (zero)
  • A string is also an array.
  • An array can be considered a list of objects. @('Joe', 'Fred', 'John') is an array with three items.

Array Type

  • The type displayed is Object [];is an array containing objects
  • [Int] is an array with only integers in it. When you create a strongly typed array, it can only contain values or objects of the specified type.

Open scripts

  • A script is a file with some code in it.
  • Make a file with the extension ps1
  • Two ways to open:1-PSDemoscript.ps1, 2-Rt click on file and select PowerShell 7.
  • You can use Visual Studio Code

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser