Powershell Info
Powershell Info
What is an object?
Objects are based on classes - a class is the blueprint used to construct the object. It’s
similar to a blueprint used to describe a house. Example: a file has properties, such as
size, name, location, and permissions. This makes the file unique and recognizable.
There are actions needed we need to perform on that files, such as read, write, move or
copy.
To find version:
$PSversiontable
Note that 3 and 4 can run scripts from 2, but not obviously if you use commands from 3
or 4, 2 can’t run it. And, 2 is the most used out there, as it comes with Win 7. 8 has 3.0,
8.1 has 4.0.
DON’T INSTALL 4.0 ON THE FOLLOWING:
System Center 2012 configuration manager (not including SP1)
System Center Virtual Machine Manager 2008 R2 (including SP1)
MS Exchange Server 2007
Windows Small Business Server 2011 Standard
Powershell runs under Normal User privilege. For some functions, you may need to run
as Administrator.
Update help every once in awhile, using the update-help command. This must be done
in Administrator mode.
To set up the execution policy so that you can actually DO stuff, you have to type:
Set-ExecutionPolicy RemoteSigned
Enable-PSRemoting
PowerShell profiles
$Profile will show you the path to your profile. To see if you HAVE a path, type: test-
path $Profile. If it returns False, then you need to create one.
If you type it exactly like that, you’ll get the #4 profile and it will create the
WindowsPowerShell directory in the user Documents folder. It will be a blank file.
# Display greeting
Clear-Host
Write-Host " "
Write-Host " "
Write-Host "`t `t `t ====================== Attention! ======================
"
Write-Host "`t `t `t You are being tracked. We know who you are. Resistance is
useless. "
Write-Host " "
Write-Host " "
NOTE: ‘ is the escape sequence. NOT a single quote, it’s the accent grave to the right
of the 1 on the top row of the keyboard. “t” is for tab.
Cmdlets
Cmdlets perform an action, and typically return an MS Net Framework object to the next
command in the pipeline (“pipeline” covered below.)
Cmdlets have the same basic structure: Verb - Noun - parameter argument. The verb
describes the action to take place, the noun describes the target of the action, and the
parameters are optional characteristics or a property of the noun.
Cmdlet syntax - when using “help”, it will list the syntax. Anything in a bracket is
optional.
Start with the command “get-help” followed by the command name: get-help get-
process, for example.
To get information on one page: get-help <cmdlet>, or help <cmdlet>
To get information on multiple pages: <cmdlet> -?
To see examples of the command: get-help <cmdlet> -examples
To see more detailed info: get-help <cmdlet> -detailed
To see full technical information: get-help <cmdlet> -full
To open in a window (PS 3.0 and up): get-help <cmdlet> -ShowWindow. In the window,
you can search for terms and they’ll be highlighted. You can click on “settings” and it
will filter out whatever you uncheck, such as syntax or description.
There are hundreds of cmdlets in PS. The command get-command will list all of them.
Combines with get-help <cmdlet>, it’s a start on learning them.
PS has a set of cmdlets that let you control which properties are displayed for particular
objects.
The cmdlets begin with the verb Format.
So, the “autosize” command, added at the end, will fix that. It will begin with the
leftmost column:
Get-Process -Name powershell | Format-Table -Property Path,Name,Id,Company -
AutoSize
Now, that might not seem very handy if you’re only looking at one process. But when
you look at ALL of them:
Get-Process | Format-Table -Wrap -AutoSize -Property Name,Id,Path -GroupBy
Company
Get-member
To change a method:
$m.toupper()
The “()” - open and close brackets - is used with methods. For a property, which in this
case is length, it would be:
$m.length
Objects
.Net objects have pre-programmed functionalities. That’s why a simple command like
$m=”Hello” will be recognized as a string object, with $m being considered a string
variable. Now PS will automatically assign a number of pre-programmed functionalities
(which can be found with the $m | Get-member command) to it.
Objects are based on classes, which is the top of the “food chain” in .Net
Framework. .NF is actually just a huge collection of classes! They define the objects.
Compare it to a blueprint for a series of houses. Each house may be a bit different, but
structurally they will all be the same, following the blueprint. Substitute “blueprint” for
classes, and “houses” for objects, and it makes sense.
So, it’s important to learn which classes are “parent” classes, that pass functionalities
down to other classes. For example, the “string class” inherits functionalities from its
parent, the “object class”. Again, objects are based on classes. So a string object is
created based on a string class. So it automatically has whatever functionalities are in
the string class, and any parent classes. This lets you use built in functions instead of
creating the wheel all over again for common things you’d want to do.
Functions are actually methods and properties. Methods are actions that can be
performed (Copy, Trim, Upper, Lower). Properties are attributes that describe the
object (Name, color, size, length).
So cmdlets are PS commands that you use to make things happen - like creating
a .NET object! Then you can use the properties and methods to create powerful
functionalities and still write very little code at all! Get-member will show you what
properties and methods are available.
To see what MLClass.dll contains, you type the variable and pipe it to get-member:
$ml | get-member
TypeName: MLClass.Messages
Name
MemberType
Definition
Equals
Method
bool Equals(System.Object o
GetHashCode
Method
int GetHashCode()
GetType
Method
type GetType()
SendMsg
Method
void SendMsg()
ToString
Method
string ToString()
IsAdmin
Property
bool IsAdmin {get;set;}
IsOnDuty
Property
bool IsOnDuty {get;set;}
Get-ChildItem
This is basically a dir command. It can be accessed by get-childitem, dir, ls, or gci (get-
childitem)
Using the -recurse option allows you to see all subdirectories and files within a folder.
Using the -path option allows you to specify a path - including the registry!
gci -path hklm:\Software will show that branch of the registry.
Using the -include option allows you to specify particular file types. For example:
c:\>gci c:\*.* -include *.txt
will list all the text files in that directory (root). If you combine with -recurse, you can
check the whole drive, but -recurse has to come first: gci c:\*.* -recurse -include *.txt