Learn about Windows PowerShell
This week we are following the Scripting Wife as she pushes ahead with her preparations for the 2010 Scripting Games. You can catch up with her progress by reviewing the articles in the archive.
Microsoft Scripting Guy Ed Wilson here. I woke up this morning, checked my e-mail, and there was a message from Michael, one of my friends in Hamburg, Germany, who has agreed to be a judge for the 2010 Scripting Games. His e-mail caused me to think about one of the things I miss about Hamburg, besides all of my friends there, and that is currywurst. One currywurst stand I found during a walkabout in Hamburg advertises that theirs is sharp.
Of course, there are many great things in Hamburg other than currywurst. It is a great place to take pictures, such as this one overlooking one of the canals toward the town square in front of the city hall.
I was just about to start a slideshow on my computer to look at all of the pictures I had taken during my last trip over to teach a Windows PowerShell workshop, when the Scripting Wife came in and plopped down in a chair next to me.
“What is an environment?” she blurted out without ceremony. “In relation to environmental variables—I have wasted the last hour trying to make heads or tails out of the links you posted in your review guide. I am more confused now than I was before I started. Why do you geeks always have to make things so hard? Not only that but I am downstairs trying to figure this stuff out, and you are up here looking at a bunch of pictures,” she said vehemently.
“Well, my dear,” I said calmly, “I can look at pictures because I already know how to work with environmental variables.”
At this point in the conversation, it would be best to skip forward a little bit. Suffice it to say the gist of her harangue was “do you want to share that information with me”—or words to that effect. Ahem.
“Well of course my love, I would be glad to assist you in learning about environmental variables,” I soothed. “Environmental variables are used by the computer to configure certain settings, such as the search path for programs or to report information such as the number of processors it has.”
“Okay, that makes sense, I guess,” she said.
“Let’s take a look at them. By using Windows PowerShell, we can easily find all of our environmental variables. To do this, type the dir env: command and press ENTER,” I said.
“Wow. It displays a lot of stuff,” she said.
“Yes, it does. You can see the name of your computer, the number of processors it has, and even which server logged you onto the network,” I said.
“Well, that is pretty cool. Now suppose I only wanted one of those variables. How would I get that instead of having to search through this big list of stuff?” she asked.
“That is easy. You just type the name of the environment variable after $env. It looks like $env:computername and press ENTER to get the name of your computer. You could do the same thing for all of the other environmental variables,” I said.
“I am not sure why I need the $env first, but who cares as long as it works,” she said. One question I do have is this: I have seen that a lot of the things Windows PowerShell returns are complicated to use. That is, they are more than just text. What about this computer name?” she asked.
“You are right. Windows PowerShell returns objects and not just plain text. Let’s see what is coming back for the computername variable. To do this, put parentheses around the command and then call the gettype method. It will look like ($env:ComputerName).gettype(). You could have just piped it to the Get-Member cmdlet and looked at the top of the list of members, but I wanted to show you this new command,” I said.
“Thanks a lot,” she exclaimed, adding, “Is this right?”
“That is absolutely correct,” I said.
“Most of the time, you will never need to modify any of these variables. If fact, if you were to change some of them, it could cause some programs to behave strangely,” I said.
“When would I need to modify the environment variables?” she asked with a concerned voice.
“About the only times you might need to do so would be to either modify the path variable or to create a new environmental variable,” I said.
“To create a new environmental variable, use the New-Item cmdlet. Let’s create an environmental variable named “Scripting_Games” and assign a value “cool.” To do this type New-Item –Path env: -Name Scripting_Games –Value cool and press ENTER. When you are finished with that, type dir env:s* to look for the environmental variables that begin with the letter s,” I said.
“Okay. How does it look?” she asked.
“It looks good. Now let’s modify the path variable. This is going to look a little weird, but it will be easy. We are going to use the += command, which means that we will take what exists and add something else to it. The command is $env:path += “;c:\fso” to add the c:\fso directory to the path. The += command means take what already exists, and add the next thing to it. Because a semicolon separates each folder in the search path, you need to add the semicolon first. To display the path, use $env:path to obtain the current path, and pipe it to the Format-List cmdlet. This is because the path is too long and the Format-List cmdlet will help display it. The command $env:path | fl * will display the revised search path,” I said.
“Okay, I got it,” she said. “How does this look?”
“It looks just fine. Now if you will excuse me, I was working,” I said.
“Yeah. Working on looking at pictures you took in Hamburg. Caught you, didn’t I?” she laughed.
If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at email@example.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys