Introduction to Windows PowerShell

1 2 3 4 5 6 7 8 9 10 11 12 Page 7
Page 7 of 12

There are exceptions to the normal search processing, though:

  • If your command includes an explicit path as part of the command name, PowerShell runs the specified file. For example, the command c:\bin\sort.exe would run that specific copy of sort.exe and would not try to search for sort as an alias or in the PATH locations.
  • To improve security, PowerShell will not run a .ps1 PowerShell script that it found in the current directory before it searched the PATH list. It will tell you about it, but it won’t run it. It will run only PowerShell scripts that it finds in the PATH or those to which you’ve explicitly typed a path.
  • You can run a file in the current directory by preceding its name with .\, as in .\myscript.ps1. This is an explicit path, so it satisfies PowerShell’s security requirement.
  • Even then, PowerShell might not run scripts at all, depending on additional security settings that I discuss in the next section.
  • It’s conceivable that more than one PowerShell snap-in might define a cmdlet with the same name. PowerShell searches through snap-ins from the most recently installed back toward the first installed, and the first snap-in found that defines the cmdlet name is the one that’s used. Also, you can import (load) new snap-in cmdlet modules while PowerShell is running, and the same ordering applies: Snap-ins are examined from the most recently imported toward the oldest.
  • Or, you might have defined an alias that has the same name as an existing cmdlet. If you type the aliased name, you’ll get the command that the alias specifies, not the original cmdlet.
  • To run a specific cmdlet from a specific snap-in without hitting this ordering or aliasing problem, you can precede the cmdlet name with the name of the snap-in module, followed by a backslash. For example, Microsoft.PowerShell.Utility\ Get-Date runs the Get-Date cmdlet in the Microsoft.PowerShell.Utility module even if you’ve defined an alias for Get-Date or another module has defined a cmdlet with the same name.
  • For more information on command searching, type help about_command_ precedence at the PowerShell command prompt.

I know this can seem tedious in an introduction, but you need to know the details of the search system to get your own scripts to run and to diagnose problems if someday the program that runs isn’t the one you expected.

The main thing you should take from this is that if you want to develop and use your own PowerShell scripts, you will want to create a specific folder to hold them and put that folder name into the Windows PATH environment variable. If you’ve already set up such a folder for batch files or WSH scripts, you can use the same folder for PowerShell scripts.

Redirection and Pipes

You can redirect the output of commands you run from the PowerShell prompt and in PowerShell scripts into files. The usual operators—|, >, <, >>, and so on—work as they do in the Command Prompt environment.

In PowerShell, though, remember that most cmdlets emit objects, not text. The output of these cmdlets turns into text listings only when they land in the PowerShell window, are redirected to files, or piped to standard Windows console commands. You can control the formatting of these text listings using specific cmdlets.

1 2 3 4 5 6 7 8 9 10 11 12 Page 7
Page 7 of 12
7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon