PowerShell Command Line 2017 - Easy Beginners Guide (2017)
PowerShell Command Line 2017 - Easy Beginners Guide (2017)
PowerShell
Command Line
2017 - Easy
Beginners Guide To
Write And Run
Scripts And Learn
Basic PowerShell
Commands!
Bonus Gift For You!
Get free access to your
complimentary book “Amazon
Book Bundle: Complete User
Guides To 5 Amazon
Products” by clicking the link
below.
>>>CLICK HERE TO
DOWNLOAD<<<
(or go to:
https://freebookpromo.leadpage
book-bundle/]
Copyright 2016 - All rights
reserved.
get-service | start-service
The Console
To anyone who has previously used MS
DOS the PowerShell console will be
familiar.
The command line is a black screen with
white text and a prompt down the left
hand side.
To use it you type in commands
(cmdlets) after the prompt and these are
executed after the Enter key has been
pressed.
Chapter 2 – Basic
Commands
Basic Commands
If you are conversant with DOS
commands then you will be able to use
many of the old commands that you are
familiar with.
The process remains the same. Choose a
cmdlets, type it in, add any arguments
you desire and complete the task by
depressing the Enter key.
Commands follow the naming pattern of
‘verb-noun’.
There are new PowerShell specific
cmdlets for all the old DOS commands
but it is not necessary that you use them.
One major change that PowerShell has
wrought upon itself is that cmdlets do
not actually write on the screen itself. In
fact, what they do is to give back an
object to the console, the console then
processes it and it is displayed.
This means that you don’t have to send
objects to the console, you can instead
send them to another cmdlets. This is a
technique and it has a name, which is
piping.
Let me give you an example here. You
can use the cmdlets: ‘get-itemproperty’
and use it with the name of a file, this
will display its core qualities. So it
might read something a little like this:
‘get-itemproperty
c:\windows\regedit.exe’
This command will then result in the
returning of an object to the console. You
will see only a portion of the vast array
of information that the default displays.
Piping the object to format-list all the
different bits of information can be
shown. This is the command you will
use:
‘get-itemproperty
c:\windows\regedit.exe | format-list
The | is the pipe symbol, use this for
piping. It is a separation device, which
holds apart two cmdlets, it
communicates to PowerShell that the
output of the initial command should be
sent as the input to the subsequent
command.
This piping symbol will also occur if the
result of the cmdlets is composed of a
few objects, they will then be processed
individually, on a case by case basis and
may have the appearance as indicated in
the example below:
‘dir | get-itemproperty | format-list’
The overall conclusion that comes at the
end is a selection of files and
directories, piped, neatly, in formation
into the cmdlets get-itemproperty.
Each property is piped to a format-list,
an array of the exact file information.
The previous example displays a trio of
commands, each one connected by
pipes.
Another example could illustrate the
point with pertinence. This piece of
code should be an excellent illustration.
There is an out-file cmdlet, which has
piped to it information from a file, it then
gives back the text to the console that
can be seen on the screen. Like this:
‘dir | get-itemproperty | format-list | out-
file info.txt’
Chapter 3 - Scripting
Scripting
A script is a text file with the extension
.ps1. Text files like these will contain
strings of calls to cmdlets. This is an
alternative to working directly in the
console.
One great advantage of working with
scripts rather then in the console is that
you don’t have to execute each command
immediately after typing it. What this
does is it enables pre-processing by
PowerShell, which means that facilities
often used in programming, such as
variables, user-defined functions, and
conditional statements.
There are two methods of running a
PowerShell Script once you have
written it. Which one you use depends
on the location of the script.
1. For a script that is located in the
current directory this is the format:
./example.ps1
2. If the script is contained in a folder
then the full path is required:
c:\powershell\example.ps1
Running unsigned scripts can damage the
system, so Windows will not allow them
to be executed. This means that you have
to configure your system to run scripts
written by you, and simultaneously,
block scripts that are downloaded from
the internet.
By using this command you can achieve
this:
‘ set-executionpolicy remotesigned ’
This is the same example as used in the
above console tutorial. The purpose of
the command gives an example of the
outputting of detailed information for
each file that is contained in the current
directory.
The text is as follows:
‘ $list = dir
Get-itemproperty $list | format-list | out-
file out.txt ’
The difference between this script and
the console action is that what issues
from ‘ dir ’ is not piped directly into
the ‘ get-itemproperty ’ but is initially
stored as a variable. This variable
named list becomes declared, after this
it is used in PowerShell and it is by
prefixing a dollar sign that this is
achieved.
It is entirely possible to make the
creation of the script happen in Notapad.
If you do this then you can save the file
as, for instance, ‘ example.ps1 ’ .
In order to run the script you then return
to the console and you inscribe on the
console the file path.
After doing this you issue the execute
command: Enter.
This will then complete the action and
the command will run as you want it to.
Typing it into the console should look
like this:
.\example.ps1
This facilitates the possibility of
rerunning of the script on multiple
occasions. In this case you don ’ t have
to re-type the script again and again, you
simply enter the file path and hey presto
the task is executed in the same manner.
You can even run the same script on as
many different computers as you want
simply by saving the file to a portable
memory device, or similar file transfer
system.
This example illustrates the inherent
adaptability of scripting for PowerShell.
In this example the directory listing is
stored in the variable list, the difference
here is that the process is individually
each time, and by the script and not by
the cmdlet.
Lets take a look at the script here:
‘ $list = dir
foreach ($item in $list) {
$fn = $item.name + "_.txt"
get-itemproperty $item | format-list |
out-file $fn
}’
By using the foreach loop it runs
through each item, then the directions
contained in between the curly brackets
{} become newly processed for each
item.
The process then ends up being
something like this: the current item gets
attributed to the variable item. An
object each item is, it outlines the
properties, each of a file or of a
directory.
New variables are created, is becomes
what it is by having the filename
attributed to it. And this is the substance
of ‘fn’. An underscore is an addition, as
is a ‘.txt’
An argument then ensues, it is the ‘get-
itemproperty’ one. This collects the
properties in much detail of each one
and passing.
What has been got back is piped, it is
piped into a format list, you then
achieve a detailed look at what it is.
In conclusion the items of the property
are exported to a new text file. This is
done by the use of the filename that was
used and calculated on the above line in
the list.
A real life situation in which you might
actually want to use something like this
is when you need to create a lot of text
files. A code such as the one lucidly
explicated above might be used to
extract a lot of files created exactly by
this script.
Another clear example of how you
might do this can be found here, a
complete tyro could easily pick this up
within say, ten minutes, it really is that
quotidian and basic.
‘$list = dir
foreach ($item in $list) {
$fn = $item.name + "_.txt"
if (test-path $fn) {
del $fn
}
}’
A slight difference here is the
conditional statement ‘if’, also the
cmdlet ‘test-path’, these alight on a
novel way of examining the ontology of
the file, if it is said to have digital
substance then the script will eliminate
the file, eternally.
1. Variables
The main point here of writing and
running a script is to make the computer
system run a task over and over again,
and complete this is a manner than is
identical each turn.
There are two aspects here. One is to
exemplify the action. This is the
movement that is going to be performed,
this is the crux of the matter and your
definition of it.
The second will see you ensure that this
action is capable of being performed, in
the same manner, on a different object.
This is where variables come into the
picture.
A variable is a blank space and it is in
this space that you enter the value of the
thing that you want to change. In here
you can put words, numbers etc.
The symbol for a variable in
PowerShell is a prefix of the dollar
sign: $
By using the dollar sign you are making
a declaration of the variables. These
might look like this:
$number = 12345
$location = ‘San Francisco’
$name = ‘Michael’
$listofnumbers = 1,2,3,4,5
$date = 3/8/17
Looping
There are many types of looping
procedures that you can perform, here is
the beginners guide. This will get you up
and running loops in no time. There are
two main types of loop detailed here.
The first is called Do While and serves
as a perfect introduction to the looping
process. The second takes you into
slightly deeper territory, it is called For
Each and allows you to set a task for
PowerShell to perform on any given set
of values.
Do While
One example of a loop is Do While, this
is one of the simplest and therefore
easiest loops to perform in PowerShell.
You can learn how to do this very easily
if you follow steps set out here.
A definition of a loop might run as
follows: a code that performs a specific
task and does that task over and over
again in a repetition of the same process.
This is why it is called a loop, because
it performs the same task over again in a
loop, hence then name: loop.
It will end up doing this task until a
condition changes the process and
disrupt the task and then it will cease
doing the task. Either this or until the set
of files and things have been completely
used up and no longer have any to do.
The Do While script uses a typically
ingenious method of naming a process,
one often used by computer science
boffins. It describes the task by saying
what it does. So in this case we can
suppose, with absolute clarity, that the
process is one that will be performed
until you reach a pre-set condition. It
might be regarded as a shortening of the
phrase do while you are commanded not
to.
As with most things on PowerShell the
are two ways to do this. These are
details of how to successfully perform
both, so that you have a great and full
understanding of what it is you are
undertaking to complete.
1. Do followed by a left curly bracket
on the same line:
DO {
This is a method that will enable you to
complete the task rather efficiently. You
will want a set of commands to be
repeated and this repetition can go on for
as long as you want. You will also set up
a condition that will be in place for the
conclusion of the repetition, indeed it
will bring it about. The task will be
repeated as many times as you want it to
be.
After writing the DO and the left curly
bracket {, you will got to the next line.
On this new line you will write the
cmdlets that you want to run. Finally, you
will put a right curly bracket } on the
line below and append the word While
and a pair of normal brackets () inside
of which you will write your conditional
statement. This statement must be binary,
either true or false, anything that is
ambiguous will not be understood.
This is an example of the Do While
loop:
Set a variable, say the number 1. Setting
a variable must be done using the dollar
$ sign and equals. This Do While loop is
set up to add 1 to the number in the
variable until it reaches the number 10.
It will look something like this:
Do {
$numbers = $numbers + 1
Write-Host "The current value of the
variable is $numbers"
} While ($numbers –lt 10)
For Each
The second looping method is called For
Each. For Each works by isolating
individual items from a larger set and
analysing the component parts on a case
by case basis. You then should augment
this process by setting the performance
of an action or group of commands to be
acted out upon the process.
As an example we might take a task in
which you need to isolate a group of
items in your directory. So we might take
something such as might be used by the
department or employees who deal with
HR at a particular company. They might
be instituting a process that takes action
on certain colleagues from a large
database. The task might be to cancel the
active processes that act upon the
account of each employee automatically,
without prompt from a human agent.
In this case perhaps the employees are
ones who have resigned, been sacked,
retired, or are no longer part of the
company for whatever reason. These
might be employees who have deserted
the employment of the company within a
given amount of time previous to the
institution of the action.
This is an eventuality that you can use
the loop For Each to complete. In order
to complete this action you need to use
the curly brackets again { Open the
sequence with the list of users. Then hit
the words For Each and the then follow
it with a parenthesis in which you should
write the words: user in that list, so that
it follows on from the ForEach tag and
makes sense if you were to read it out.
Next use, on the line below, a left curly
bracket {
Next you need to specify what it is you
are looking to accomplish, so on the next
line you might write ‘disable process X
on the users above so that it stops
permanently’. After this you need to hit
the close curly bracket and that looks
like this }.
So the whole thing would be set out as
below in the window you can see:
ForEach (user in that list)
{
disable process X on the users above
so that it stops permanently
}
Piping
You can use piping in almost all places
on Powershell. The piping does not take
place in the interstices of the commands
but rather takes the objects and pipes
them.
The symbolic representation of piping,
as you will have already seen, is: |
This is the same symbol as has been
used in previous versions of IT
administration, however, in this case
there is something new involved with
PowerShell.
Utilization of the cmdlet Out-Host gives
you information taken from the a position
of another command and displays the
information in a normal text format, as
lines of text and data. This can be
confusing and poor for quick reference.
By using the Out-Host cmdlet in
conjunction with the Paging command
on the occasion of having a lot of
information coming from the output, you
can use the piping cmdlet in order to
cimply things a little bit.
Processes that take a lot of juice from
the computer can really benefit here.
This is all about the display of the
processes in the console window. The
console window is required to display
its information in text format, this
representation of what is really
computing can be very difficult and takes
time to sift through.
The information is beamed to the cmdlet
Out-Host when the system has
something to display. The previous
cmdlets can halt the process at this point
until the next piece of information is
available for display.
To illustrate this try running this script in
your PowerShell console:
Get-Adultitem C:\Windows - Recurse
And then compare it to this one:
GetAdultitem C:\Windows - Recurse |
Out-Host -Paging
Notice the use of the piping symbol in
the second code.
This is a great way to simplify your
processes and can save you so much
time it is unreal.
Commanding Piping
The main thing to remember when
becoming a fully fledged pipe
commander is that PowerShell is an
object focussed console.
The user has the distinct advantage of
being able to ascertain the package
related information that becomes
available. A specific extraction can then
take place in order for the correct
information to be loosed; it really is a
great way of accomplishing the task that
you have set yourself.
For instance the Get-Location
command, which is a commonly used
cmdlet, can be used in a variety of ways.
It sends back information resulting from
a different path. This information goes
by the name of PathInfo and it is an
object. The computer than decides how
to display things and does it in a way
that is great for all concerned.
PowerShell helps you to send
information to an alternative output,
which is an invaluable tool when
completing IT administration tactics.
Conclusion