POWERSHELL BASICS

advertisement
POWERSHELL BASICS
BACKGROUND

Powershell is a task automation and scripting language based off
the .NET framework

It provides the user full access to the COM (Component Object
Model) and WMI (Windows Management Instrumentation)

The functions (lightweight commands) that PowerShell utilizes to
complete certain tasks are called cmdlets (command-lets)
BACKGROUND (CONTINUED)

Cmdlets are not standalone executables; they are instances of
.NET framework classes

PowerShell syntax is case insensitive
FIRST SCRIPT

Suppose you wanted to query a list of currently running processes
on a system. The following command will do just that:

Get-Process

To sort the list of processes, use the pipeline `|` to pass the output
to the Sort-Object cmdlet (or Sort)

You can select certain fields of the output by piping the output
and passing it using select <field name>
FIRST SCRIPT (RUNNING THE SCRIPT)

There are various ways a PowerShell script can be run

Command line


Type in the path of the script

If the current directory is the same as the script, the filename must
be preceded with “.\” or “./”
File Explorer
FIRST SCRIPT (RUNNING THE SCRIPT)



Based on the ExecutionPolicy setting on the system, this may
need to be overridden in order to be able to run scripts
To change this setting, you must be an administrator
You can bypass this setting without actually changing the setting
by writing a one-line batch file that calls PowerShell to run the
script using certain arguments
ACTIVE DIRECTORY

PowerShell, being native to the Windows environment, is a
powerful language to use when interacting with AD objects

The user can process information more quickly than some GUI
utilities
ACTIVE DIRECTORY (EXAMPLES)

Query all AD users in the domain:



Get-ADUser –Filter *
Get AD users based on a filter:

Get-ADUser -Filter 'name -like "parsa*“’

Notice the wildcard at the end of the filter
Filter users based on department:

Get-ADUser –Filter ‘office –like “*<department name string>*”’
ACTIVE DIRECTORY (EXAMPLES, CONTINUED)

You can query AD computer objects that have been created or
modified by a certain date

$d = (Get-Date).addDays(-1)

Variable $d is assigned the value of one day before the current
date
UPDATING COMPUTER SCRIPT

./updateWorkstation.bat

./updateWorkstation.ps1
GET AD COMPUTERS WHEN CREATED SCRIPT

./getComputerModBy.bat

./getComputerModBy.ps1
BACKUP USERS SCRIPT

./backup.bat

./backup.ps1
GET COMPUTER NAME AND RAM SCRIPT

./getComputerName.bat

./getComputerName.ps1
SOURCES

http://windowsitpro.com/powershell/top-10-active-directorytasks-solved-powershell

https://msdn.microsoft.com/en-us/library/ms714395(v=vs.85).aspx
Download