What Are Console Commands?
A handful of what I like to call console
commands stem from the good ol’ DOS days. They have been available in
command prompts since then, with the current PowerShell consoles
included. These commands provide standard directory navigation, file
handling, and screen handling.
Several common console commands follow:
• cd
: Changes the directory
• cls
: Clears the console screen
• dir
: Displays a listing of the current directory (folder)
• type
: Used with a text-based filename and displays the contents of a text file
Tip
These commands have been grouped within PowerShell as Aliases. To see all of the available aliases use Get-Alias
.
Path Environment Variable
Scenario/Problem:
You have scripts within a local folder which you want PowerShell to
recognize such that you do not need to use the full path name.
Solution: Add the local directory path to the PSModulePath variable.
You add the local folder’s path to the PSModule
environment variable by entering the following command in the PowerShell command prompt (substituting <folder path>
with the local directory path), as shown in Listing 2.2.
Listing 2.2. Adding a Folder Path to the PSModulePath
$env:PSModulePath = $env:PSModulePath + ";c:\<folder path>"
Tip
Add the same exact command line into
the PowerShell profile configuration file to always have your local
folder added to the PSModulePath.
Running Unsigned Scripts
Scenario/Problem: You need to be able to run unsigned scripts within PowerShell.
Solution: Use the Set-ExecutionPolicy
command.
To run unsigned scripts, you must change the execution policy:
Set-ExecutionPolicy remotesigned
You are prompted with a confirmation. Enter Y
and press Enter (or just press Enter—Y
is the default).
In some cases, you must set the policy to Unrestricted:
Set-ExecutionPolicy unrestricted
Disabling the Confirmation Prompt
Scenario/Problem: You need to automatically confirm operations such that a script runs unattended.
Solution: Include the -Confirm
parameter with a $false
setting.
When running cmdlets ,
you might receive a confirmation message to confirm the action being
processed. If you use these cmdlets in scripts, the script will not run
straight through without prompting for the confirmation.
The cmdlets that have a -Confirm
optional parameter can be called in unattended mode by passing in -Confirm:$false
to the cmdlet, as shown in Listing 3.
Listing 3. Suppressing the Confirmation Prompt Example
Remove-item *.png -Confirm:$false
Note
Not all cmdlets have a –Confirm
parameter. Use the help (-?)
parameter to verify each cmdlet in question.
Generating Inline Credentials
Scenario/Problem: You need to provide cmdlets with credentials without prompting for them.
Solution: Create a new PSCredential
object.
Various cmdlets require SharePoint and/or SQL Server account credentials for proper
authentication when performing the desired operations. The examples
include using (Get-Credential)
as the parameter value, which prompts the user for credentials.
When running these cmdlets in scripts, the
prompting for credentials pauses the execution. If you need the script
to run straight through, instead of using Get-Credential
, you can generate a new PSCredential
object inline, as shown in Listing 4.
Listing 4. Generating Inline Credentials Example
(New-Object System.Management.Automation.PSCredential "domain\user",
(ConvertTo-SecureString "password" -AsPlainText -Force))
Therefore, simply substitute (Get-Credential)
for the text shown in Listing 2.4 with the proper username and password when using a cmdlet with authentication requirements.
Referencing an Assembly
Scenario/Problem: You want to be able to call methods and/or use objects within a trusted assembly (DLL).
Solution: Use a reflection declaration, and then use a variable to instantiate an object.
You can reference an assembly in your
PowerShell script and then use any objects available by assigning them
to a variable. Then you can use any methods or properties within your
script. No intellisense exists, so you need to know the object model
beforehand. Listing 5 demonstrates an example assembly reference and usage.
Listing 5. Referencing and Using a SharePoint Assembly
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.
SharePoint")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.
SharePoint.Administration")
$spFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$spFarmSolutions = $spFarm.Solutions
Tip
Instead of coding and compiling a console application, use a PowerShell script that references the SharePoint assemblies.