Download presentation
Presentation is loading. Please wait.
1
Cmdlets “Command-lets”
PowerShell Cmdlets “Command-lets” Copyright © 2016 – Curt Hill
2
Introduction Previously, we considered:
Similarity of PowerShell to the DOS Command Box Scripting basics Scripting flow of control Now we examine in more detail the notion of a cmdlet These are lightweight commands that perform an action and usually return a .NET object Copyright © 2016 – Curt Hill
3
Differences From Commands
Cmdlets are instances of .NET Framework classes Not stand-alone executables Cmdlets usually do not parse, display errors, or format output This is handled by PowerShell Cmdlets process input objects rather than streams of text Cmdlets are record-oriented because they process a single object at a time Object may contain several things Copyright © 2016 – Curt Hill
4
Form Cmdlets use a verb-noun pair to identify a cmdlet
These are followed by options or items to act upon An alias may exist that makes the cmdlet resemble a previous DOS command Lets consider the copy-item cmdlet Copyright © 2016 – Curt Hill
5
Copy-Item Takes two strings as parameters
The copy from string The copy to string Both should refer to one or more files Wildcards may be used The aliases include Cp Copy Cpi Options include -recurse Copyright © 2016 – Curt Hill
6
Aliases These are provided to make it more convenient for users
Especially from other systems The COPY-ITEM cmdlet has as aliases the following: CP – for UNIX/LINUX users CPI – just an appreviation COPY – for DOS users Copyright © 2016 – Curt Hill
7
Item In the previous cmdlet there were two pieces What is an Item?
The verb was Copy The noun was Item What is an Item? It could be: Variable File Among others Copyright © 2016 – Curt Hill
8
Item Verbs Get – gives entries for files Move – Copy + delete original
Similar to get-childitem, which also shows subdirectories If you give it a variable it interprets as file Move – Copy + delete original New – creates a new file Use –type directory for a new directory Remove – deletes Use –recurse to remove subdirectories Rename – Guess what this does Copyright © 2016 – Curt Hill
9
Assignment Not only will a cmdlet do some useful action
It may also be used to produce values for variable It can be saved in variable using: $files = get-childitem Wildcards could be used after get-childitem Copyright © 2016 – Curt Hill
10
Piping As in DOS the vertical bar (|) is the pipe directive
This transfers the output of one program into the input of another This is frequently done with cmdlets as well We will see examples later Copyright © 2016 – Curt Hill
11
Perhaps Useful Cmdlets
Add-content file data Append the data to the end of a file Clear-content file Empties a file, but does not erase Compare-object Shows what is in one or other Does not show both Two should be same type Copyright © 2016 – Curt Hill
12
More Cmdlets Get-content file Set-content file Test-path file
Gets the file content Set-content file Creates file if needed Test-path file Returns true if file exists Wildcards may be used Write-host Displays strings to console Copyright © 2016 – Curt Hill
13
Content The {Add|Clear|Get|Set}-Content only works well for text files
Any specialized format will likely be damaged by Add, Clear or Set and return garbage for Get Copyright © 2016 – Curt Hill
14
Convert to HTML Yes this is a cmdlet
Any command that produces a display may be piped into this The result may be manipulated further or set into a file Here are two screen shots Copyright © 2016 – Curt Hill
15
Creating HTML Copyright © 2016 – Curt Hill
16
Viewing Copyright © 2016 – Curt Hill
17
Help The cmdlet get-command lists all the cmdlets available on this system There are typically hundreds Use Help and the cmdlet name to find out more about each If you need more help still use the –online parameter This will take you to a web site Copyright © 2016 – Curt Hill
18
More Help You can find the files in a directory
Use get-item or get-childitem What can be done with this information? Pipe it into get-member This will show the properties and methods Copyright © 2016 – Curt Hill
19
Properties and Methods
Any variable may contain an object of any number of types It depends how the variable was set Get-member shows the available properties and methods A property or method is used by following the item with a dot and then giving the property or method name A method must be followed by parenthesis Usually empty Copyright © 2016 – Curt Hill
20
Example In this example the following will be done
The get-item cmdlet will find the information on .PPT files in a directory It will assign to a variable The properties and methods of that data will be shown Two properties and one method will be exercised This will use 5 screen shots Copyright © 2016 – Curt Hill
21
Assigning Copyright © 2016 – Curt Hill
22
Finding Members Copyright © 2016 – Curt Hill
23
Finding Again Copyright © 2016 – Curt Hill
24
Properties Copyright © 2016 – Curt Hill
25
Methods Copyright © 2016 – Curt Hill
26
Finally As you can see there is lots more to know
What we need now is a demonstration Then an exercise We will need to look at parameters and scope Then an assignment Copyright © 2016 – Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.