In: Categories » Computers and technology » Windows Vista » What is Microsoft PowerShell Part 1
Microsoft PowerShell is a new programming language with capabilities far beyond the older command interpreters that were included with previous versions of Windows.
Getting Used to .PS1 Scripts Released at nearly the same time as Windows Vista, Microsoft PowerShell is an extensive script language that is far richer than the batch language included with previous versions of Windows. PowerShell can also be downloaded and run on Windows XP and Windows 2003, if these operating systems are updated to a recent service pack. This means PowerShell scripts you write can also run on most other Windows PCs in operation today. If you think, as we do, that PowerShell is kind of a silly name, go ahead and imagine that PS stands for Microsoft’s Programming Shell. Aside from the hyped-up marketing moniker, the language is quite serious. PowerShell represents so much capability that scripts for Microsoft’s Exchange 2007 e-mail application are written in it. You probably won’t need to write anything that complex. But no matter the size of your project, Microsoft has built PowerShell so it has few limitations. It’s worlds away from Windows’ old batch language, which is run by the
Cmd.exe command interpreter. As just one of the features PowerShell brings to Windows that Microsoft’s older commandline environments did not, you can use PowerShell commands to read from and write to hives in the Windows Registry as though they were ordinary drives. For example, you access
HKEY_LOCAL_MACHINE through a drive named
HKLM and
HKEY_CURRENT_USER via
HKCU. PowerShell scripts normally bear an extension of
.ps1, for PowerShell version 1. This contrasts with Windows’ built-in
Cmd.exe, which runs batch files ending in
.bat. Presumably, if a future PowerShell version 2 is released, scripts that require the more advanced language will bear the extension
.ps2, and so on. Microsoft wisely avoided making
.ps1 files execute automatically if you happen to open one. Under a new Microsoft policy announced before Vista was released, a potentially harmful feature is not turned on by default unless 90 percent of Windows users would use it. Almost everyone sends files to a printer, for example, and this isn’t particularly dangerous - so printing capabilities are automatically enabled in Vista. Running scripts, however, can silently install worms or Trojan horses if you inadvertently open one. Hacker web sites and untrustworthy e-mails will undoubtedly try to use PS scripts to slip rogue programs onto users’ PCs. To give you an idea of the problem, the
.ps1 extension sounds like it might have something to do with PostScript. You can imagine virus writers sending out mass e-mails after Vista becomes widely installed, saying things like, “See the naughty pictures in the attached PostScript file.” Noticing the unfamiliar
.ps1 extension, many users might indeed assume the attachment was a harmless image file. In reality, although a racy picture might be displayed, a powerful script could also be silently launched, infecting a user’s PC. Keeping PowerShell disabled, except for those users who need such scripts and turn them on, is Microsoft’s way of minimizing the damage. If you know what you’re doing, enabling PowerShell isn’t particularly dangerous. If you don’t need PowerShell, however, you don’t need it. Viruses can’t run rampant via PowerShell scripts if most Windows users have PowerShell turned off. To run PowerShell safely if you do use it, be sure to see the Secret in this article on running only digitally signed scripts.
Running PowerShell Scripts Safely Microsoft disables by default the operating system’s ability to run PowerShell scripts merely by clicking or opening them. But, if you use PowerShell scripts, you should take the following three steps to protect yourself against hacked web sites or e-mails that might find ways to run them without your knowledge.
1. Obtain a digital certificate and digitally sign any scripts you develop in-house. Require that scripts you obtain from other parties be signed by them.
2. Configure PowerShell so it will run only those scripts that are signed by parties you know and trust.
3. Secure your list of trusted certificates. It doesn’t do any good to restrict scripts to a list of trusted parties if a virus or Trojan horse is able to quietly add other signatures to your list. If you don’t know how to take the preceding steps, get a professional to set up your system in this way - or don’t configure your system to run any scripts that happen to get opened.
Constructing a PowerShell Command We can’t adequately document the PowerShell language in this article - entire articles deal with this subject, and we recommend that you study one if you want to become proficient. Instead, we offer here an introduction and a Quick Reference that we hope you’ll find useful. The PowerShell language draws from several older programming languages. Its grammar is based on POSIX, a character-based command language that’s also called a shell (because it sits over the operating system kernel). POSIX is a subset of the Korn Shell, which is widely used in Unix environments. PowerShell, however, is not a copy of POSIX. Instead, it adopts many of the features of the Microsoft .NET object model. In a nutshell, pardon the expression, .NET objects have properties, which are defined in well-understood ways. For example, a file object has a size in bytes, a date and time when the file was last modified, a read-only flag (which may be on or off), and so forth. In creating the commands that would be available in PowerShell, Microsoft’s developers used a verb-noun naming convention. Some common PS commands are:
Get-ChildItem
Get-Content
Remove-Item In each case, the first word is a verb, followed by a hyphen and then a noun on which the verb operates. (Speaking of case, all PS commands are case-insensitive.
GET-childITEM does the same thing as
get-childitem. We’re showing commands here with initial capital letters for the same reason Microsoft does - to improve readability.) These commands seem awfully long - and you haven’t seen anything yet. Fortunately, short versions called aliases are built in for most commands. You can also define your own aliases. The three PS commands previously shown are equivalent to the following
Cmd.exe commands that you probably know:
dir
type
del That’s right - Microsoft has defined long, hard-to-remember commands to replace
dir (which shows a directory listing of filenames),
type (which displays the contents of a file), and
del (which erases files). Microsoft explains the length of its new PS commands by saying it helps developers understand them. It also makes it possible to get a synopsis of a group of commands by using the command
get-help. Um, well, actually, almost everyone will use the command’s short, memorable alias instead:
help. For example, you can see a 1-line listing of all the commands that get information:
help get-*
Name Category Synopsis
------------------------- -------- ------------
Get-Acl Command Get the access control list ...
Get-Alias Command Returns alias names ...
Get-AuthenticodeSignature Command Gets the signature object ... You can also see those commands that operate on objects:
help *-object
Name Category Synopsis
-------------- -------- ------------
Compare-Object Command Compares the properties of ...
ForEach-Object Command Applies script blocks to each ...
Group-Object Command Groups the objects that contain ...
Seeing All the Commands PowerShell provides a command, known as
Get-Command, that displays all the built-in commands that are available. Unlike the old
Cmd.exe, which relies on external, on-disk files for most of its features, PowerShell has a wealth of internal commands. These are known as Cmdlets, pronounced commandlets. The alias for
Get-Command is
gcm. In our tests, both the Cmdlet and its alias required the addition of a pipe (|) and the
more command to keep the output from scrolling off the top of our screen. The following lines show how to use the
more command:
gcm | more
CommandType Name Definition
----------- ----------- ---------------------------
Cmdlet Add-Content Add-Content [-Path] ...
Cmdlet Add-History Add-History [[Input-Object] ...
Cmdlet Add-Member Add-Member [-MemberType] ... If you often use batch commands or PowerShell scripts, you’ll probably want to expand the character-based window in which the commands appear. The Layout tab of the Command Prompt Properties dialog box will enable you to do just that ( 22-1).
legal notice
Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.
Useful tools and features
If you like this article (tutorial), please link to it from your web page using the information above.
related articles
Apple users have long been able to take advantage of the Mac OS X Dashboard, and Windows users have been able to download Yahoo Widgets (formerly Konfabulator Widgets). Those things are still available, but now Vista has its own little tools, known as gadgets. Vista gadgets live in the new Windows Sidebar which you can move to the left or right side of the screen by right-clicking it and selecting Properties. Or you can put Gadgets on your Desktop by dragging the little context menu that appears when you hover ...
2. Support for RSS News Feeds in Windows Vista
IE 7.0 includes an easy way to subscribe to news feeds, regularly updated information that sites publish in the format known as Really Simple Syndication (RSS). When a surfer visits a site that publishes one or more news feeds, a square broadcast icon on IE 7’s toolbar changes from grey to orange. Clicking the icon takes you to a page that explains the content of a feed and provides a clickable link that subscribes you. This is a big improvement over previous news feed buttons in other browsers, which formerly ...
3. Thinking of Cheating at Solitaire in Windows Vista
Unfortunately, Vista new Solitaire code seems to have broken one way that neerdowells have cheated at the game for years. This scandalous behavior was first revealed in Windows 3 s all the way back in 1991. As that article explained it, you could click Game - Undo when playing a Draw Three game, and the last three cards you turned over from the deck would go back on the pile. If you then held down the Shift key while clicking the deck, only one card at a time would turn over, allowing you to ...
4. A Quick Overview of All the Versions of Windows Vista
It seems like Windows Vista has a lot more versions than Microsoft has ever offered before. But that isn’t quite true. The Redmond company years ago split Windows XP into almost as many versions as we have today with Vista. You may occasionally hear Vista’s versions referred to as SKUs. This term stands for Stock Keeping Unit. We’ll use the more common terms version and product version throughout this article instead. Here’s a review of the major Windows XP versions (rough...
Windows Anytime Upgrade Unlike previous versions of Windows, Vista installs itself with the capability to upgrade from a weaker version to a more-capable version at any time. You simply run the Anytime Upgrade applet, select a source to purchase an upgrade license from, and your PC is quickly enhanced with the more powerful version you’ve selected. _ Vista Home Basic can be upgraded in this way to Home Premium or Ultimate. _ Vista Home Premium and ...
6. Deploying Windows Vista: A Power User`s Toolkit
If you’re an enterprise administrator faced with the prospect of rolling out Windows Vista to hundreds or thousands of desktops around the world, take heart: Microsoft has finally upgraded its deployment tools in dramatic fashion, taking advantage of the componentized architecture of Windows Vista. But these deployment tools aren’t just advantageous to the world’s biggest corporations. If you’re a power user, a tinkerer, or someone who ends up having to reinstall Windows fairly regularly, you mi...
7. What`s New in the Windows Vista User Interface
Gazing upon Windows Vista for the first time, you will immediately be struck by how different everything looks when compared to older Windows versions such as Windows XP and Windows 2000. Now, windows are translucent and glass-like, with subtle animations and visual cues. This new interface leaves no doubt: Windows Vista is a major new Windows version, with much to learn and explore. In this article, we’ll examine the new Windows Vista user interface, called Aero, and explain what you need to ...