MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))

Windows PowerShell can be used as a replacement for the CMD interpreter. Its many built-in cmdlets allow for large number of activities. These cmdlets can be used in a stand-alone fashion, or they can be run together as a group.

Accessing Windows PowerShell

After Windows PowerShell is installed, it becomes available for immediate use. However, using the Windows flag key on the keyboard and pressing the letter r to bring up a run command prompt, or “mousing around” and and using Start | Run | Windows PowerShell all the time, becomes somewhat less helpful. I created a shortcut to Windows PowerShell and placed that shortcut on my desktop. For me, and the way I work, this is ideal. This was so useful, as a matter of fact, that I wrote a script to do this. This script can be called through a logon script to automatically deploy the shortcut on the desktop. The script is called  CreateShortCutToPowerShell.vbs, and is as follows:

 CreateShortCutToPowerShell.vbs

Option Explicit Dim objshell Dim strDesktop Dim objshortcut Dim strProg strProg = "powershell.exe" Set objshell=CreateObject("WScript.Shell") strDesktop = objshell.SpecialFolders("desktop") set objShortcut = objshell.CreateShortcut(strDesktop & "\powershell.lnk") objshortcut.TargetPath = strProg objshortcut.WindowStyle = 1 objshortcut.Description = funfix(strProg) objshortcut.WorkingDirectory = "C:\" objshortcut.IconLocation= strProg objshortcut.Hotkey = "CTRL+SHIFT+P" objshortcut.Save Function funfix(strin) funfix = InStrRev(strin,".") funfix = Mid(strin,1,funfix) End function

Configuring Windows PowerShell

Many items can be configured for Windows PowerShell. These items can be stored in a Psconsole file. To export the Console configuration file, use the Export-Console cmdlet, as shown here:

PS C:\> Export-Console myconsole

The Psconsole file is saved in the current directory by default and has an extension of psc1.

The Psconsole file is saved in an xml format. A generic console file is shown here:

<?xml version="1.0" encoding="utf-8"?> <PSConsoleFile ConsoleSchemaVersion="1.0"> <PSVersion>1.0</PSVersion> <PSSnapIns /> </PSConsoleFile>

Controlling PowerShell launch options

  1. Launch Windows PowerShell without the banner by using the -nologo argument. This is shown here:

    PowerShell -nologo

  2. Launch a specific version of Windows PowerShell by using the -version argument. This is shown here:

    PowerShell -version 1

  3. Launch Windows PowerShell using a specific configuration file by specifying the -psconsolefile argument. This is shown here:

    PowerShell -psconsolefile myconsole.psc1

  4. Launch Windows PowerShell, execute a specific command, and then exit by using the -command argument. The command itself must be prefixed by the ampersand sign (&) and enclosed in curly brackets. This is shown here:

    powershell -command "& {get-process}"

Категории