0% found this document useful (0 votes)
83 views

KM4 1.1 Introduction To Scripting & Windows Command-Line

This document provides an overview of a coding and logic course that explores scripting on Windows and Linux operating systems. The course covers topics like command line basics, scripting languages, web development, and exam preparation over the course of a week. It describes scripting as code that is executed line by line by an interpreter, unlike compiled languages. It also discusses advantages and disadvantages of using the command line interface.

Uploaded by

abdul shahid
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

KM4 1.1 Introduction To Scripting & Windows Command-Line

This document provides an overview of a coding and logic course that explores scripting on Windows and Linux operating systems. The course covers topics like command line basics, scripting languages, web development, and exam preparation over the course of a week. It describes scripting as code that is executed line by line by an interpreter, unlike compiled languages. It also discusses advantages and disadvantages of using the command line interface.

Uploaded by

abdul shahid
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Coding & Logic

The purpose of this knowledge module is to:

• explore ways of configuring and managing computer systems


• use command-line environments on both Windows and Linux operating
systems
• understand the basic concepts of scripting
• know the purpose of various types of algorithms that can be used when
scripting
• describe the lifecycle of script development
• set up and configure web services
Coding & Logic
Monday Tuesday Wednesday Thursday
• Introduction to • Linux • PowerShell • Exam prep
scripting command-line Scripting
basics
• Windows • Scripting
command-line • Administering Lifecycle
basics Linux from the
command-line
• Administering • Web
Windows from • Bash scripting in Development –
the command- Linux HTML, CSS,
line XML, HTTP

• Batch scripting
Coding & Logic
Coding & Logic
Coding & Logic
What is a scripting language?

How is this different from any other typical


programming language?
Coding & Logic
Coding & Logic
Scripting means the code you write is executed or run one line at a
time. The software which runs the code interprets the meaning of
each command as the script is running.

This is different to languages such as C++ and C# which are compiled


once the code has been written, and converted into a binary
executable file.

Before we can begin to create scripts, we first need to fully explore


the range of different actions that can be performed at the
command-line.
Coding & Logic
When using the command-prompt has advantages:
• When you want to quickly do something you know how
to do it, rather than wade through menus.
• When you want to repeat a certain task lots of times, or
at some point in the future, e.g.
• Make a backup of files
• Delete unneeded temporary files
• Map network drives or resources via logon scripts
• If you want to administer a machine remotely
When using the command-prompt has disadvantages:
• When you don’t know the name of the command to do something, or what options
to make it work the way you want
• When the thing you want to automate requires user input, e.g. selecting a default
printer
Coding & Logic
The Windows command prompt dates back to the DOS operating system of
the 1980’s.
It allows a user to navigate the file system, launch and terminate processes,
perform maintenance, and automate tasks using batch files.
The process running inside the window is c:\windows\system32\cmd.exe
Coding & Logic
The Windows PowerShell was introduced in 2006.
It allows for easier automation of tasks and configuration of the operating
system. It contains a built-in scripting language based on the .NET
Framework.
Additional commandlets can be downloaded and installed to extend
functionality.
Coding & Logic
Coding & Logic
Operating systems based on UNIX such as Linux (Ubuntu, Debian, Fedora,
etc) and Mac OS X have a different type of command-shell environment.

Examples include Bash, Dash, Sh, Ksh, etc


Windows Command Prompt
Coding & Logic
Task 1
Let’s re-familiarise ourselves with the basics of the
Windows command prompt.

1) Open a command prompt


2) Change directory to the root of the C: drive
3) View the directory contents
4) Change the directory to c:\Windows
5) View the directory contents
Windows Windows
CommandCommand
Prompt Prompt
Coding & Logic
Task 1
Let’s re-familiarise ourselves with the basics of the
Windows command prompt.
1) Open a command prompt
• Start / Run / cmd
2) Change directory to the root of the C: drive
• cd c:\ or cd \ or cd .. several times
3) View the directory contents
• dir
4) Change the directory to c:\Windows
• cd c:\Windows
5) View the directory contents
• dir
Windows Command Prompt

Task 2
Let’s re-familiarise ourselves with the basics of the
Windows command prompt.

1) Open a command prompt


2) Make a new directory in the root of the C:\ drive called “Driver Backups”
3) Copy all .sys files from “C:\Windows\system32” to “c:\Driver Backups”
Windows Command Prompt

Task 2 - Solution
Let’s re-familiarise ourselves with the
basics of the Windows command prompt.
1) Open a command prompt
• Start / Run / cmd
2) Make a new directory in the root of the C:\ drive called “Driver
Backups”
• Mkdir “C:\Driver Backups” or md “C:\Driver Backups”
3) Copy all .sys files from “C:\Windows” to “C:\Driver Backups”
• Copy C:\Windows\system32\drivers\*.sys “C:\Driver Backups”
Windows Command Prompt

Task 1 & 2 – Lessons Learnt


Whilst completing these tasks you may
have noticed that:
• There are multiple different ways to launch a command prompt. It always starts in your user
profile directory.
• Both the mkdir and md command can be used to create a new dir
• When working with directory or file names that contain a space, the path should be enclosed
in “quotes”
• Different commands require different parameters. For example, mkdir requires just one (the
name of the new dir), whilst copy requires two (the source and destination). Dir will list the
contents of any path specified as a parameter, but if none is specified, it defaults to the
current dir.
• The asterisk (*) character is a wildcard that matches any text
• The Windows command prompt is not case-sensitive. It does not matter if you use upper or
lowercase letters.
Windows Command Prompt

Other Features & Shortcuts


To make your life easier when using the
command prompt, there are a few features
to make note of:

• Use the up and down arrows to cycle through previously entered commands
• Use the tab key to auto-complete when typing paths.
• Press F7 to see a list of previously entered commands
• Press F8 to match whatever you have partially typed against previously
entered commands
Windows Command Prompt

Task 3 - Variables / Identifiers


There are a number of environment variables which can
be used to recall information about the operating system:
1) To view the contents of these, use the echo command with the following
parameters
%SystemRoot% %HomeDrive% %ComputerName%
%UserName% %Number_Of_Processors% %PATH%
%UserProfile% %OS%

2) To view all available environment variables, simply type set.


3) Create your own variable
• set test=hello
• echo %test%
4) Open a new command prompt. Does the %test% variable still exist?
Windows Command Prompt
Internal and External Commands
Whenever you enter a command, the interpreter
tries to match what you have typed to a recognised
command.

• Internal commands are built into cmd.exe and do not exist as separate
executable files
• cd cls color copy date del dir echo exit for goto if md mkdir mklink
move path pause prompt rd rmdir rem ren set start time title type
• External commands are executable files that exist on the operating
systems’ storage device
• Diskpart ipconfig netstat
Windows Command Prompt
Internal Commands

Cd Change directory Mklink Make a shortcut


Cls Clear the screen Move Move a file
Color Change colours Path Set the %PATH%
Copy Copy a file Pause Wait for a key press
Date Show the current date Rd / rmdir Delete a dir
dir Directory listing rem Comment out a line
Echo Display text Ren Rename a file
Exit Close the command prompt Set Set a variable
For Run commands in a loop Start Launch a new instance
Goto Jump to a section of a script Time Display the time
If Perform different actions Title Change the window title
Md / mkdir Make a directory type Display contents of a file
Windows Command Prompt
Internal and External Commands
Whenever you enter a command, the interpreter tries to
match what you have typed to a recognised command.

1) Did the user type in a full path to an executable? E.g.


• “C:\program files\Steam\steam.exe”
If yes, then run that executable. If not, then go to next step
2) Did the user type an internal command?
If so, run that command. If not, then go to next step.
3) This must be an external command. Is the external command in the current
working directory?
If so, run that command. If not, look for it in each location specified in the
%PATH% variable
Windows Command Prompt
Getting Help
Most commands will display information on how to use them if you use /? as the only
parameter.
Commands use letters to represent
E.g. copy /? different options or switches that specify
how they should work. They are
enclosed in [brackets] because they are
optional – you do not have to use them.

Commands usually also require


parameters to tell them what to do
something with. These may or may not
be optional. The copy command has a
mandatory source parameter but
optional destination parameter.
Windows Command Prompt

copy /Y /V C:\Windows\system32\drivers\*.sys “C:\Driver Backups”

Commands use letters to represent


different options or switches that specify
how they should work. They are
enclosed in [brackets] because they are
optional – you do not have to use them.

Commands usually also require


parameters to tell them what to do
something with. These may or may not
be optional. The copy command has a
Can you identify the command, the switches/options, mandatory source parameter but
and the parameters in the above example? optional destination parameter.
Windows Command Prompt

copy /Y /V C:\Windows\system32\drivers\*.sys “C:\Driver Backups”

Commands use letters to represent


different options or switches that specify
how they should work. They are
enclosed in [brackets] because they are
The command being run is “copy”, optional – you do not have to use them.
e.g. to copy files from one location to
another Commands usually also require
parameters to tell them what to do
something with. These may or may not
be optional. The copy command has a
Can you identify the command, the switches/options, mandatory source parameter but
and the parameters in the above example? optional destination parameter.
Windows Command Prompt

copy /Y /V C:\Windows\system32\drivers\*.sys “C:\Driver Backups”

Commands use letters to represent


different options or switches that specify
These two switches control the how they should work. They are
behaviour of the command. enclosed in [brackets] because they are
• /Y forces overwriting if the optional – you do not have to use them.
destination file already exists Commands usually also require
• /V verifies that the file copied parameters to tell them what to do
correctly something with. These may or may not
be optional. The copy command has a
Can you identify the command, the switches/options, mandatory source parameter but
and the parameters in the above example? optional destination parameter.
Windows Command Prompt

copy /Y /V C:\Windows\system32\drivers\*.sys “C:\Driver Backups”

Commands use letters to represent


different options or switches that specify
how they should work. They are
enclosed in [brackets] because they are
This is the first parameter the optional – you do not have to use them.
command needs. This tells it what
files we want to copy. Commands usually also require
parameters to tell them what to do
something with. These may or may not
be optional. The copy command has a
Can you identify the command, the switches/options, mandatory source parameter but
and the parameters in the above example? optional destination parameter.
Windows Command Prompt

copy /Y /V C:\Windows\system32\drivers\*.sys “C:\Driver Backups”

Commands use letters to represent


different options or switches that specify
This is the second parameter. This how they should work. They are
enclosed in [brackets] because they are
tells it where to copy the files to. optional – you do not have to use them.

Note that it is enclosed in speech Commands usually also require


marks as it has a space in the name. parameters to tell them what to do
something with. These may or may not
be optional. The copy command has a
Can you identify the command, the switches/options, mandatory source parameter but
and the parameters in the above example? optional destination parameter.
Windows Command Prompt Task 4 - Try using a couple of different switches
with the copy command.
1) Copy all Windows .sys files to “C:\Driver Backups”. You should notice that you are
asked to confirm if you want to overwrite the existing files – since we did this
previously.
• Copy C:\Windows\system32\drivers\*.sys “C:\Driver Backups”
Windows Command Prompt Task 4 - Try using a couple of different switches
with the copy command.
1) Copy all Windows .sys files to “C:\Driver Backups”. You should notice that you are
asked to confirm if you want to overwrite the existing files – since we did this
previously.
• Copy C:\Windows\system32\drivers\*.sys “C:\Driver Backups”
2) Repeat the above command using the /Y switch
• Copy /Y C:\Windows\system32\drivers\*.sys “C:\Driver Backups”
Windows Command Prompt Task 4 - Try using a couple of different switches
with the copy command.
1) Copy all Windows .sys files to “C:\Driver Backups”. You should notice that you are
asked to confirm if you want to overwrite the existing files – since we did this
previously.
• Copy C:\Windows\system32\drivers\*.sys “C:\Driver Backups”
2) Repeat the above command using the /Y switch
• Copy /Y C:\Windows\system32\drivers\*.sys “C:\Driver Backups”
3) Repeat the above command using the /Y and /V switch
• Copy /Y /V C:\Windows\system32\drivers\*.sys “C:\Driver Backups”
Windows Command Prompt
Redirecting Output
By default, the output of commands is displayed on screen – this is known as standard
output, or stdout. However, this can be redirected to a file using the > and >> symbols.
Task 5
1) Get a directory listing of “C:\windows” sorted by size of files and save to output.txt
• Dir /o:s c:\windows\system32 > output.txt
2) Now do the same for “C:\program files” and save to output.txt
• Dir /o:s “c:\program files” > output.txt
3) View the contents of the file
• Notepad output.txt
• Type output.txt
• Note that the second command overwrote the output from the first command
4) Repeat the task but use >> instead of >
Windows Command Prompt
Piping Output
Standard output can also be redirected into the standard input of another command, using
the pipe symbol | (Shift key and the key to the left of Z)
Task 6
1) View the contents of the file output.txt
• Type output.txt
2) Pipe the output to the find command to search for specific text
• Type output.txt | find “.exe”
3) Run the command again but this time redirect the output to searchresults.txt
• Type output.txt | find “.exe” > searchresults.txt
Windows PowerShell
The Windows PowerShell has all of the features and functionality of the command
prompt, plus lots more.

There are several ways to access PowerShell.

1) Select “Windows PowerShell” for the 64 bit version, and


“Windows PowerShell (x86) for the 32 bit version

2) Alternatively, use Windows PowerShell ISE. This is an


Integrated Scripting Environment, which gives you more
help and assistance when typing commands, and helps
you create and run PowerShell scripts.
Windows PowerShell
Windows PowerShell
Task 7
1) Open PowerShell (NOT the ISE) as
administrator
2) View list of available commands
• Get-command
3) Look at the list of available commands –
there are lots! Some of these are
functions and some are cmdlets.
• Functions are PowerShell scripts that
perform a certain action
• Cmdlets are compiled programs that
do something, just like external
commands in the regular command-
prompt
Windows PowerShell
Task 8
1) Find the edition of Windows we are using. The “-online” parameter means
the currently installed version of Windows, rather than pointing the
command to an installation image file.
• Get-Windowsedition -online
Windows PowerShell
PowerShell ISE allows you to work in
two different ways:

• Type individual commands in the


bottom blue panel and run them
just as you would in regular
PowerShell
• Type a sequence of commands in
the top white panel and run them
whenever you want by pressing
the green arrow. To access this:
• Go to View menu
• Select Script Pane
Windows PowerShell
Task 9
1) Open Windows PowerShell ISE as
an administrator and run the
same command
• Get-Windowsedition –online
• Notice that as soon as you
type Get- a popup menu
appears suggesting
commands you could use
Windows PowerShell
Task 10
Commands also have properties which can be used to do things with their output
1) View list of available commands
• Get-command
2) Count how many total commands there are
• (get-command).count
Windows PowerShell
Task 11
The output of a command can be saved in a
variable so that it can be used later, as part of
another command
1) Type the following code in the top panel
• $NumCmds = (Get-command).count
• Write-Host “There are $NumCmds
commands available to use”
2) Press then green run icon at the top
Quiz

When using a command-line interface, what is the purpose of a


switch?

a) To enable or disable features of the operating system


b) To change the way a command behaves
c) To provide specific information needed by a command
d) All of the above
Quiz

When using a command-line interface, what is the purpose of a


switch?

a) To enable or disable features of the operating system


b) To change the way a command behaves
c) To provide specific information needed by a command
d) All of the above
Quiz

When using a command-line interface, what is the purpose of a


parameter?

a) To set the minimum or maximum number of times to do


something
b) To change the way a command behaves
c) To provide specific information needed by a command
d) All of the above
Quiz

When using a command-line interface, what is the purpose of a


parameter?

a) To set the minimum or maximum number of times to do


something
b) To change the way a command behaves
c) To provide specific information needed by a command
d) All of the above
Quiz

When using the Windows command-prompt, how can you find


help for a specific command?

a) Put “--help” after the name of the command


b) Use the “man” command
c) Use the “Get-Help” command
d) Put “/?” after the name of the command
Quiz

When using the Windows command-prompt, how can you find


help for a specific command?

a) Put “--help” after the name of the command


b) Use the “man” command
c) Use the “Get-Help” command
d) Put “/?” after the name of the command
Quiz

When using the Windows PowerShell, how can you find help for
a specific command?

a) Put “--help” after the name of the command


b) Use the “man” command
c) Use the “Get-Help” command
d) Put “/?” after the name of the command
Quiz

When using the Windows PowerShell, how can you find help for
a specific command?

a) Put “--help” after the name of the command


b) Use the “man” command
c) Use the “Get-Help” command
d) Put “/?” after the name of the command
Quiz

Which of the following would display the contents of an


environment variable in a Microsoft Windows operating system?

a) Echo %Username
b) Echo $Username
c) Echo %Username%
d) Echo $Username$
Quiz

Which of the following would display the contents of an


environment variable in a Microsoft Windows operating system?

a) Echo %Username
b) Echo $Username
c) Echo %Username%
d) Echo $Username$
Quiz

Which of the following commands would perform a directory


listing and save the output in a text file, overwriting it if it already
exists?

a) Dir c:\windows | output.txt


b) Dir c:\windows output.txt
c) Dir c:\windows >> output.txt
d) Dir c:\windows > output.txt
Quiz

Which of the following commands would perform a directory


listing and save the output in a text file, overwriting it if it already
exists?

a) Dir c:\windows | output.txt


b) Dir c:\windows output.txt
c) Dir c:\windows >> output.txt
d) Dir c:\windows > output.txt
Quiz

Which of the following best describes what this command would


do? Copy c:\windows\* c:\backups

a) It would copy everything from c:\backups to c:\windows


b) It would copy all files from c:\windows to c:\backups
c) It would copy all directories from c:\windows\ to c:\backups
d) It would copy everything from c:\windows to c:\backups
Quiz

Which of the following best describes what this command would


do? Copy c:\windows\* c:\backups

a) It would copy everything from c:\backups to c:\windows


b) It would copy all files from c:\windows to c:\backups
c) It would copy all directories from c:\windows\ to c:\backups
d) It would copy everything from c:\windows to c:\backups
Quiz

Which of the following symbols can redirect the output of one


command into another command?

a) |
b) ?
c) *
d) >
Quiz

Which of the following symbols can redirect the output of one


command into another command?

a) |
b) ?
c) *
d) >

You might also like