
Running a Script Inside the Exchange Management Shell
Those familiar with the Cmd.exe environment know how to run command shell scripts. These are nothing more that text files that have the .bat file name extension. Like batch files, you can create the Exchange Management Shell script files by using a text editor, such as Notepad. The Exchange Management Shell script files use the .ps1 file name extension.
The Exchange Management Shell uses a root directory for script files when they are called. By default, the root directory is the <root drive>:\Program Files\Microsoft\Exchange Server\bin directory. You can also verify the current PSHome directory on any computer that is running the Exchange Management Shell by running $PSHome from the command line. Both of these directories are in the PATH environment variable.
If a script file is saved to the root directory, you can call it by using the script name. If the script file is located somewhere other than the current location, the path and script name must be used. If the script file is located in the current location, the script name must be prefixed by .\.
The following examples show the command syntax requirements for calling three different scripts. These examples all use the Get-Date cmdlet, from three different locations.
[PS] C:\>Get-Date-Script-A.ps1
Friday, January 20, 2006 3:13:01 PM
The script file Get-Date-Script-A.ps1 is located in the directory that is specified by $PSHhome and requires only the script name to run.
[PS] C:\>c:\workingfolder\Get-Date-Script-B.ps1
Friday, January 20, 2006 3:13:25 PM
The script file Get-Date-Script-B.ps1 is located in the C:\workingfolder directory so the full path must be supplied to run.
[PS] C:\>.\Get-Date-Script-C.ps1
Friday, January 20, 2006 3:13:40 PM
The script file Get-Date-Script-C.ps1 is located in the current location, C:\. Therefore, it must be prefixed with .\ to run.
[PS] C:\>Get-Date-Script-C.ps1
'Get-Date-Script-C.ps1' is not recognized as a Cmdlet, function, operable program, or script file.
At line:1 char:21
+ Get-Date-Script-C.ps1 <<<<
In the last example, when this same script, Get-Date-Script-C.ps1, is called without the prefix .\, the expected results are shown.
As a best practice, always give script files a descriptive name and include comments in the script to describe its purpose and to identify each point of interest. Some information about the author should also be included in case someone who is running the script has questions about its use. Use the pound symbol (#) to start comment lines inside the script body.