Premkumar Yogeswaran's Blog

Active Directory | PowerShell | DNS | DHCP | Exchange Server | VM Ware

Archive for the ‘PowerShell’ Category

PowerShell Scripts and Commands

Powershell Commands

Posted by Premkumar Yogeswaran on January 23, 2013


a
 Get-Acl Get permission settings for a file or registry key
 Set-Acl Set permissions
 Get-Alias gal Return alias names for Cmdlets
 Import-Alias ipal Import an alias list from a file
 New-Alias nal Create a new alias.
 Set-Alias sal Create or change an alias
 Get-AuthenticodeSignature Get the signature object associated with a file
 Set-AuthenticodeSignature Place a signature in a .ps1 script or other file
c
 Set-Location cd/chdir/sl Set the current working location
 Get-ChildItem dir/ls/gci Get child items (contents of a folder or registry key)
 Get-Command gcm Retrieve basic information about a command
 Measure-Command Measure running time
 Trace-Command Trace an expression or command
 Add-Content ac Add to the content of the item
 Get-Content cat/type/gc Get content from item (specific location)
 Set-Content sc Set content in the item (specific location)
 Clear-Content clc Remove content from a file/item
 ConvertTo-Html Convert the input into an HTML table
 ConvertFrom-SecureString Convert a secure string into an encrypted standard string
 ConvertTo-SecureString Convert an encrypted standard string into a secure string
 Clear-Host clear/cls Clear the screen
 Clear-Item cli Remove content from a variable or an alias
 Copy-Item copy/cp/cpi Copy an item from a namespace location
 Get-Credential Get a security credential (username/password)
 Get-Culture Get region information (language and keyboard layout)
d
 Get-ChildItem dir/ls/gci Get child items (contents of a folder or registry key)
 Get-Date Get current date and time
 Set-Date Set system time on the host system
 Get-PSDrive gdr Get drive information (DriveInfo)
 New-PSDrive mount/ndr Install a new drive on the machine
 Remove-PSDrive rdr Remove a provider/drive from its location
e
 Get-Eventlog Get eventlog data
 Get-ExecutionPolicy Get the execution policy for the shell
 Set-ExecutionPolicy Change the execution policy (user preference)
 Export-Alias epal Export an alias list to a file
 Export-Clixml Produce a clixml representation of powershell objects
 Export-Console Export console configuration to a file
 Export-Csv epcsv Export to Comma Separated Values (spreadsheet)
 Invoke-Expression Run a PowerShell expression
 Exit Exit Powershell
f
 ForEach-Object foreach Loop for each object in the pipeline
 ForEach Loop through values in the pipeline
 Format-Custom fc Format output using a customized view
 Format-List fl Format output as a list of properties, each on a new line
 Format-Table ft Format output as a table
 Format-Wide fw Format output as a table listing one property only
g
 Get-Item gi Get a file/registry object (or any other namespace object)
 Get-ChildItem dir/ls/gci Get child items (contents of a folder or registry key)
h
 Get-Help help Open the help file
 Add-History Add entries to the session history
 Get-History history/h/ghy Get a listing of the session history
 Invoke-History r/ihy Invoke a previously executed Cmdlet
 Get-Host Get host information
 Clear-Host clear/cls Clear the screen
 Read-Host Read a line of input from the host console
 Write-Host Display objects through the host user interface
i
 if Conditionally perform a command
 Import-Clixml Import a clixml file and rebuild the PS object
 Import-Csv ipcsv Take values from a CSV list and send objects down the pipeline.
 Get-Item gi Get a file object or get a registry (or other namespace) object
 Invoke-Item ii Invoke an executable or open a file (START)
 New-Item ni Create a new item in a namespace
 Remove-Item rm/del/erase/rd/ri/rmdir Remove an item
 Set-Item si Change the value of an item
 Clear-ItemProperty Delete the value of a property
 Copy-ItemProperty Copy a property along with its value
 Get-ItemProperty Retrieve the properties of an object
 Move-ItemProperty Move a property from one location to another
 New-ItemProperty Set a new property of an item at a location
 Remove-ItemProperty Delete the property and its value from an item
 Rename-ItemProperty Rename a property of an item
 Set-ItemProperty Set the value of a property
k
 Stop-Process kill/spps Stop a running process
l
 Get-Location pwd / gl Get and display the current location
 Pop-Location popd Set the current working location from the stack
 Push-Location pushd Push a location to the stack
 Set-Location cd/chdir/sl Set the current working location
m
 Add-Member Add a member to an instance of a PowerShell object
 Get-Member gm Enumerate the properties of an object
 Move-Item move/mv/mi Move an item from one location to another
o
 Compare-Object Compare the properties of objects
 Group-Object group Group the objects that contain the same value for a common property
 Measure-Object Measure the properties of an object
 New-Object Create a new .Net object
 Select-Object select Select properties of objects.
 Sort-Object sort Sort objects by property value
 Where-Object Filter the objects passed along the command pipeline.
 Out-Default Send output to default
 Out-File Send command output to a file
 Out-Host oh Send the pipelined output to the host
 Out-Null Send output to null
 Out-Printer lp Send the output to a printer
 Out-String Send objects to the host as strings
p
 Powershell Launch a powershell session
 Convert-Path cvpa Convert a ps path to a provider path
 Join-Path Combine a path and child-path
 Resolve-Path rvpa Resolves the wildcards in a path
 Split-Path Return part of a path
 Test-Path Return true if the path exists, otherwise return false
 Get-Pfxcertificate Get pfx certificate information
 Pop-Location popd Set the current working location from the stack
 Push-Location pushd Push a location to the stack
 Get-Process ps/gps Get a list of processes on a machine
 Stop-Process kill/spps Stop a running process
 Clear-ItemProperty clp Remove the property value from a property
 Copy-ItemProperty cpp Copy a property along with it's value
 Get-ItemProperty gp Retrieve the properties of an object
 Move-ItemProperty mp Move a property from one location to another
 New-ItemProperty Set a new property
 Remove-ItemProperty rp Remove a property and its value
 Rename-ItemProperty rnp Renames a property at its location
 Set-ItemProperty sp Set a property at the specified location to a specified value
 Get-PsProvider Get information for the specified provider
 Set-PSdebug Turn script debugging on or off
 Add-PsSnapIn Add snap-ins to the console
 Get-PsSnapin List PowerShell snap-ins on this computer
 Remove-PSSnapin Remove PowerShell snap-ins from the console
r
 Read-Host Read a line of input from the host console
 Remove-Item rm/del/erase/rd/ri/rmdir Remove an item
 Rename-Item ren/rni Change the name of an existing item
 Rename-ItemProperty Rename a property of an item
s
 Get-Service gsv Get a list of services
 New-Service Create a new service
 Restart-Service Stop and then restart a service
 Resume-Service Resume a suspended service
 Set-Service Change the start mode/properties of a service
 Sort-Object sort Sort objects by property value
 Start-Service sasv Start a stopped service
 Stop-Service spsv Stop a running service
 Suspend-Service Suspend a running service
 Start-Sleep sleep Suspend shell, script, or runspace activity
 Select-String Search through strings or files for patterns
t
 Tee-Object Send input objects to two places
 New-Timespan Create a timespan object
 Trace-Command Trace an expression or command
 Get-Tracesource Get components that are instrumented for tracing.
 Set-Tracesource Trace a PowerShell component
 Start-Transcript Start a transcript of a command shell session
 Stop-Transcript Stop the transcription process
u
 Get-Uiculture Get the ui culture information
 Get-Unique gu Get the unique items in a collection
 Update-Formatdata Update and append format data files
 Update-Typedata Update the current extended type configuration
v
 Clear-Variable clv Remove the value from a variable
 Get-Variable gv Get a powershell variable
 New-Variable nv Create a new variable
 Remove-Variable rv Remove a variable and its value
 Set-Variable set/sv Set a variable and a value
w
 Where-Object where Filter input from the pipeline
 While (condition) {action} else {action}
 Get-WMIobject Get WMI class information
 Write-Debug Write a debug message to the host display
 Write-Error Write an object to the error pipeline.
 Write-Output echo Write an object to the pipeline
 Write-Progress Display a progress bar
 Write-Verbose Write a string to the host's verbose display
 Write-Warning Write a warning message
 # Comment / Remark

Posted in Active Directory, PowerShell | Leave a Comment »

PowerShell Ramp Up Guide

Posted by Premkumar Yogeswaran on July 28, 2012


I want to go through the following:

  • Understanding PowerShell
  • Understanding of Basic Concepts
  • PowerShell in Practice
  • Technology-specific PowerShell
  • Breaking News
  • Resources

Understand PowerShell

As I suspect is the case for most of you reading this, I started off using PowerShell as an IT Admin before I was employed by MS. I was driven by a need to accomplish a task, but once I’d started learning the language, the possibilities showed themselves all over the place. So the first piece of advice I’d give is to find a problem to solve, and solve it. This may be as simple as translating an existing script from another language into PowerShell, or it may involve writing something that will manage your entire estate for you while you put your feet up – note, if it’s the latter, I will require a copy sending through…

The first key point is to understand what PowerShell is, and what it’s not:

  • PowerShell is more than just a scripting language; it’s a full object-oriented programming language. This does mean that any prior programming experience will be beneficial, but it’s simple in its implementation, and uses common concepts.
  • It is consistent, and chock full of descriptions, syntax guides and examples.
  • It is designed to help admins and others wishing to script tasks, and the built-in help is designed in this way, to assist admins who aren’t familiar with programming techniques to get the most out of it.
  • It is not going anywhere – it is applied across most current MS products, and will be in new ones for the foreseeable future, which means that once a technique is acquired, this can be rinsed and repeated across Windows Server, AD, Exchange, SQL and many other technologies, and any investment of time or money spent learning it will reap rewards for a long time to come.
  • Most importantly of all, it is not scary, and it’s not difficult to quickly start writing your first scripts.

Understanding of Basic Concepts

With the above said, the next step is to begin to understand PowerShell and its basic concepts:

Something a little more hands on:

And a structured course including demos and labs:

PowerShell in Practice

Once you’ve achieved a level of comfort with PowerShell itself, you’ll want to start putting it in to practice. Each major Server Technology ships with its own PowerShell module or snap-in, so there is one for AD, Exchange, Sharepoint etc. These are outside of the scope of this article, but there will be some links at the bottom to help you to find resources to learn about these.

In order to start using PowerShell usefully, here are some server management specific resources:

You’ve probably also got scripts written in VB that you would like to convert to PowerShell. Well, we can help with that too:

Finally, there’s a follow-up course to the one above that builds and introduces more advanced concepts:

Technology-specific PowerShell

Once the fundamentals of PowerShell are understood, applying them to any given technology is simply a case of learning the cmdlets for that technology and then implementing the techniques. Here are a few links to cmdlets for some common technologies:

Alternatively, using get-command –module <modulename> will list all the commands contained in that module, e.g. get-command –module ActiveDirectory will show all the commands loaded by the ActiveDirectory PS module

Breaking News!

Just as I was finishing up writing this post, CTP1 was released for the Management Framework 3. There’s a post on the PowerShell blog, which also links to the download, and I’d be very surprised if there weren’t some really cool features coming. Bear in mind that this is the version that will ship in Win8, so get comfortable with it now to give yourself a headstart when Win8’s released. But do yourself a favour, don’t put it on your primary machine, because it’s only pre-release code at the moment!

Resources

The MS Events site

The MS Events site is a one-stop shop for all sorts of webcasts, podcasts and virtual labs. The link is to a search for PowerShell specific posts, but there are a lot of other resources for other technologies too.

Blogs

Code Repositories

  • The Scripting Games is an event held by the Scripting Guy to provide problems to enable you to learn PowerShell. So if you’re stuck finding a problem to solve, check it out and try to solve them. Answers are available too, so you can check how you’re doing.
  • The Code Repository contains scripts submitted by both MS employees and PS enthusiasts.
  • The PowerShell Guy’s Blog is another site containing code samples and discussion.
  • PowerShell Code Repository has more for you to enjoy.

Posted in Active Directory, PowerShell | 1 Comment »