-
Notifications
You must be signed in to change notification settings - Fork 10
PowerShell Basics
#PowerShell Basics
I know there are a lot of tutorials out there, but I want anyone using gShell to have a quick and dirty tutorial of PowerShell so they can get off the ground. I may make some assumptions here to speed things along, but it's with the best of intentions.
So without further ado, let's do ado this. (Ha! It's really late and I'm not apologizing for that one.)
###Nicknames for PowerShell It's often referred to also as PoSh or PS. I'll try to continue using the full name throughout this wiki.
##Starting PowerShell There are many ways to interact with and run PowerShell. For instance, you can open a CmdPrompt (aka Dos Prompt) and just type 'powershell'. I don't recommend this unless you need something really quick. Even then I'd recommend using the next one.
There is also an ISE, which stands for Integrated Scripting Environment. It's the default way to write anything in PowerShell, and as of versions 3+ has features like auto-complete drop-down menus and built-in help windows. It's what I use most of the time.
You can also save some PowerShell in to a file and run it from the file by right clicking on it and choosing the appropriate option, but that's out of scope for any of this.
##What's a Cmdlet? A Cmdlet is the basic unit of PowerShell. If you want to do something, you have to type something in. That something is generally a Cmdlet.
You can also write code around the Cmdlets to do some advanced programmy things, but that's later.
An example of a basic, general, non-gShell Cmdlet is:
Get-Help
Get-Help is a great Cmdlet to memorize, by the way. It will help you a lot in getting information not only about PowerShell itself, but also about the gShell Cmdlets.
##Anatomy of a Cmdlet Using the above example, you'll notice the Cmdlet has a Verb-Noun structure. This is normal for any PowerShell Cmdlet. Other examples of Cmdlets with different verbs and nouns are Set-ADUser, Get-Process, New-Alias.
If you want to give the Cmdlet additional information (which is often required) you have to tell it what information you're giving it by giving it a parameter, and then you give it the information you promised it - the argument. Let's use a gShell cmdlet for an example.
Get-GAUser -UserName myuser -Domain mydomain.com
Here we've passed the Get-GAUser cmdlet two parameters containing the UserName and Domain arguments. The -UserName prepares it for the information to follow, which we provide by typing in 'myuser'. A similar thing happens with -Domain as well.
For a bit more on this, check out the Syntax Help.
##Results Once you type that in and hit enter, you'll get your result, something like this:
GivenName : Mario
FamilyName : DaPlumba
PrimaryEmail : myuser@mydomain.com
Aliases : {}
Suspended : False
OrgUnitPath : /Admin
LastLoginTime : 1/21/2015 12:00:04 PM
userObject : Google.Apis.Admin.Directory.directory_v1.Data.User
The beauty of this is that it's not plain text - what you're seeing is an object. If this were something you were running in an old CmdPrompt window, you'd see the words and that would be it. In order to do anything with it you might have to copy it out in to Excel and then manipulate it with a bunch of formulas and mumbo jumbo, and then copy that to another program, etc.
In PowerShell, the results you see are displayed to us as text representations (how else would we read it?) but under the hood the results are objects. Think of these objects like a way to describe something.
For instance, if we step away from the code for a moment, envision an apple. Now, if someone said to you 'Describe an apple object to me' you might reply 'a round, red or green fruit that grows on trees'. Congrats, you just came up with an object.
If you ran a Cmdlet, you might see the following (I made this up, don't try it):
> Describe-Fruit -Type apple
Shape : round
Colors : {red, green}
Source : tree
So, if you scroll back up and look at what you did with Get-GAUser it is basically the same thing.
##Variables
Now that you've got an idea of what the results are (objects) let's do something with them. We're going to store all of those results in a variable. A variable is like a short-hand representation of the object it is holding on to, and also means you don't have to keep asking for the same information over and over.
In PowerShell, variables can be created on-the-fly by throwing a dollar sign in front of a word, and then setting it equal to something.
$ResultUser = Get-GAUser myuser mydomain.com
Now once you run that, instead of seeing the results you would see... nothing? That's because before, when you just called the Cmdlet by itself, it had nothing else to do with the results BUT to show it to you. Now, however, you've told it to store those results in the variable $ResultUser. Since it's storing it, there's no need to show it to you.
Want to see what's in the variable? It's easy as:
$ResultUser
So what? We've made some shorthand. The nice part about this is you can now continue to do things with it. Let's get the first name of the user here, and check to see if it matches another name we have stored.
Note: You'll see me putting comments in the code below after a hashtag, number sign or pound sign #. In PowerShell, most things after a # are commented out - that means the program will ignore them.
# Here I'm creating a new variable and setting
# it equal to some text:
$Othername = "Luigi"
# Now I'll check and see if this matches the other name
$ResultUser.GivenName -eq $OtherName
In this case, I'll let you find out the answer yourself =)
Keeping in mind this is a very basic and fairly useless example, the point made is that things can be done. But that's for another time. I think for now this pretty much covers the hardcore basics that you'll need to use gShell.
News - Get Started - Cmdlet Index - FAQ - Discussion - Downloads