Join Jason C. Helmick for an in-depth discussion in this video Using variables for storage, part of Up and Running with PowerShell 5.
- This is a good time for me to show you another way to store data. And it's because when I'm working with stuff and I wanna build a script and I'm trying to work on a solution, I don't necessarily want to see the information or the results immediately. In other words, I don't wanna see this right now. I wanna store this for later and maybe manipulate it or work with it later, further down in a script or even here in the console. So we can store data to variables. Variables are temporary memory storage that get created that can store any type of data that we want.
Now here's how PowerShell's gonna make this easy. Without getting too technical on all this, PowerShell makes it easy so that you don't have to specify what kind of data you wanna store, the size of the data. It figures it all out for you. Now first of all, I would be a bad Jason if I did not show you how to find all of the commands for variables. So get-help on variable. You can see that we have several cmdlets for variables. Now here's the interesting part. You don't necessarily need to use these.
Matter of fact, I'm gonna show you the easy way to work with variables. This doesn't mean that we never use these, we do. We sometimes use these, especially in scripts to create new variables, get a variable, clear a variable. But let me show you how straight forward this can be using PowerShell. I'd like to make a variable and I'd like to store a valuable to it. A variable, you have to give it a name that's going to be unique. So let's call our variable var. I know not very creative but here's the interesting thing with PowerShell, your variable needs to start with a dollar sign.
Now this is not part of the name of the variable. This is simply alerting the parsing engine that you are creating a variable or utilizing a variable. I can set this equal to, and let's say, "Hello!" Now if I wanna see what the variable has in it, we do have cmdlets that can display what's inside of a variable or do a lot of other things too like Write-Output, is the correct one to use. And I could just say $var. But I also want you to notice that if I say $var, just type that and press Enter, it also gives me the output of that variable.
Now variables are kinda interesting, now watch. Variables are temporary storage. If I close this console, the variable goes away. It gets cleaned up. That's a feature not a flaw. That way if you're storing large amounts of data, when you close this PowerShell session, all of that goes away. Well let's go back to our variable for a second and let's do something interesting. Get-service -name bits Here's the interesting thing. Look what's inside of the variable. Not the command Get-service -name bits but the result.
And this isn't just a text result. This is a reference to the actual object. So look I can pipe this to Get-Member and look, I have the object. I have a ServiceController object. I've got all of its methods and its properties. So if you think about this, I'm putting the pointers in those objects into these variables. I can now manipulate them in a very unique way. This could make your life a lot easier. As an example, $var.status This is a dot notation that allows me to reference properties and methods on that variable.
This is interesting because I've got the status and you see the name, it's bits. And see it's currently stopped? Let's call a method on it. This is an easier way to call methods on things that are stored into variables rather them piping them to ForEach. Now you can still pipe this to ForEach, but you can also do this. Now an interesting thing, if I type $var and I ask for its status, it should be started and you're saying, well no, it says stopped. That's because this is a pointer. It's not updated.
So I can update it by using, there happens to be a refresh method that'll update the information in the variable. $var.status and now it's updated. So it is running. The usage of variables is very important in PowerShell and variables can be very, very flexible. So also you have complete access to it, variables can be as far as the .NET Framework. So if you have more experience with the .NET Framework and with variables from there, these can be true multi-dimensional arrays, jagged arrays.
We can do a simple array such as $var=1,2,3,4 There's a simple array. Let me show you what that's gonna look like. Each element in the array. It's a zero-based array. So let's say I wanted to change the number two to a number nine. Here's what the syntax would be. We put in square brackets and the number of the element that we wanna change. We start counting at zero. So zero, one, so it'd be number one. I like to check first and then we can set it to whatever we'd like.
So that's an example of using variables as an array. This can become very useful because, let's do this. $var= let's put in get-service. Let's put in all the services. Well $var will list all of them, however, I can also do this: $var I just wanna see the first service in that array. There we go. Or I wanna see the last one in that array. Now the reason that we're looking at variables now is because when you start to script, you're gonna wanna store information.
Store information about possibly what computer do you want me to go to to get things from. And that's what you're gonna start to see. Plus, we're gonna start to use variables to do some unusual things that'll help our management. We're gonna start storing some special things for something called PowerShell remoting. So as we start to script, now it's time for variables. Tell you what, as looking at variables are, I need to show you a better way to script that'll be more helpful and then we'll start to put it all together.
- Installing Windows Management Framework 5
- Running commands (cmdlets)
- Discovering commands
- Understanding cmdlet syntax
- Resolving terse commands
- Finding and using local modules
- Working with files, printers, CSVs, and XML in the pipeline
- Selecting, sorting, and filtering object data
- Creating scripts
- Automating tasks
- Using PowerShell remoting