PowerShell Language Notes
PowerShell is a command-line shell and scripting language.
Useful in automating Administration tasks, as the scripting language can perform "any group of actions".
Personal Opinion: Microsoft had to be weird and do THAT?. Even C++ and Python have shorter text display functions. What is the user input
Read-Host
?.
Enter the following command into the command line and hit Enter.
PowerShell commands are called CmdLets
pronounced command-lets. A command's name describes its function and is typically a verb or a noun.
This command has the verbs Get
and the noun Date
which indicates its purpose, being to obtain the current date, and presented in the terminal.
The
Get-Date
command uses theWrite-Host
function to display the full date and time.
This command will use the Write-Host
function to display all the CmdLets
that have the verb "Get" in the function name. This is the same with the noun parameter.
Clears the terminal session. clear
can also be used.
1) Start by outputting today’s date.
Write-Host
.Command Completion is a tool for suggesting a command based on a few letters. Triggered by the Tab
key, the terminal will attempt to autocomplete the rest.
Autocorrect suggestions follow alphabetical order. May also Display a list of potential command options..
The command
history
also works to retrieve the history for the PowerShell session.
TheUp
andDown
arrow keys can be utilised to scroll through previously used commands.
1) Type Get-
into the terminal and hit Tab
.
In the online editor of Codecademy
for PowerShell this resulted in a list of all potential options being displayed.
2) Type Get-Ti
and hit the Tab key.
This will autocorrect to Get-TimeZone
instead of displaying a list.
3) Run the command Get-Host
Write-Host
displays the following host information.
Display Name | Variable Type | Purpose |
---|---|---|
Name | String | The name of the host application (e.g., "ConsoleHost"). |
Version | System.Version | The version number of the host application. |
InstanceId | System.Guid | A unique identifier for this instance of the host. |
UI | PSHostUserInterface | An object to interact with the host's User Interface. |
CurrentCulture | CultureInfo | The culture settings used for formatting (dates, numbers). |
CurrentUICulture | CultureInfo | The culture settings used for UI text (e.g., error messages). |
PrivateData | Object | Host-specific data or custom objects. |
DebuggerEnabled | Boolean | Indicates if the debugger is currently enabled. |
IsRunspacePushed | Boolean | True if a new runspace has been entered (e.g., nested prompt). |
Runspace | Runspace | The operating environment where commands are executed. |
4) Use the up and down arrow keys to look at your command history. Choose one of the two commands you’ve already run and hit Enter. |
I chose the Get-Host
to rerun and continue the tasks.
5) Type Get-Hi
and hit the Tab key. Hit Enter to review the command history.
I typed Get-Hi
clicked tab to autocorrect to Get-History
and then continued.
To run a PowerShell Script
the file extension is .ps1
.
# Display all the commands that act on the Host itself.
Write-Host "All Commands that can Act on the Host:"
Get-Command -noun Host
This file will display the following output.
All Commands that can Act on the Host:
CommandType Name
----------- ----
Function Clear-Host
Cmdlet Get-Host
Cmdlet Out-Host
Cmdlet Read-Host
Cmdlet Write-Host
1) Print out a prompt and then the current date.
To start, on the first line of script.ps1 use
Write-Host
to output a prompt about the date. Something like The Current Date Is:.
Get-Date
to output the current date. Already done in the first task.
3) Now in the Terminal.
Current Progression:
- How PowerShell can automate our work within an operating system
- Common commands such as:
- Get-Date
- Write-Host
- Get-Command
- Get-History
- Using terminal shortcuts to increase productivity
- Writing scripts help us run complex tasks
1) run output.ps1
./output.ps1
2) Which one of the following commands will NOT help you retrieve your terminal history.
Get-Command
3) What does the command Write-Host "Good Day"
do when run in the terminal?
Prints Good Day
to the terminal.
4) Which one of the following commands will erase all the text from your current terminal session?
Clear-Host
5) Which of the following commands prints the current date when run in the terminal?
Get-Date
6) In what situation should you use PowerShell over a graphical user interface?
Automating a repetitive administration task.
7) True or False: PowerShell can only be used on the Windows operating system.
I answered True which was incorrect. I'm aware that it is Open-Source so this is unfortunate.
Incorrect. PowerShell is available for the Windows, Mac and Linux operating systems.
8) Fill in the command, so it produces the following output in the terminal:
CommandType Name
----------- ----
Function Clear-Host
Cmdlet Get-Host
Cmdlet Out-Host
Cmdlet Read-Host
Cmdlet Write-Host
I answered Get-Command -noun Host
based on drag and drop.
9) Fill in the blanks to complete the sentence.
10) Which key on the keyboard is used for terminal autocompletion
Variables & Operations - PowerShell