2025-11-28 00:35:46 +09:00

2.0 KiB

Windows PowerShell PowerShell01 Sample

This sample uses InitialSessionState to constrain a runspace and add commands and providers.

This sample will concentrate on the SDK mechanisms to restrict the runspace. Script alternatives to the SDK include $ExecutionContext.SessionState.LanguageMode and the PSSessionConfiguration cmdlets.

Sample Objectives

This sample demonstrates the following:

  1. Restricting the language by setting the InitialSessionState.LanguageMode property.
  2. Adding aliases to the environment using InitialSessionState.Commands property.
  3. Marking commands as private.
  4. Removing providers using InitialSessionState.Providers property.
  5. Removing commands using InitialSessionState.Commands property.
  6. Adding commands and providers.

Windows PowerShell

InitialSessionState

Operating system requirements

Client

Windows 8.1

Server

Windows Server 2012 R2

Build the sample

  1. Start Microsoft Visual Studio and select File > Open > Project/Solution.

  2. Go to the directory named for the sample, and double-click the Visual Studio Solution (.sln) file.

  3. Press F7 or use Build > Build Solution to build the sample.

    The executable will be built in the default \bin or \bin\Debug directory.

Run the sample

  1. Start command prompt.
  2. Navigate to the folder containing the sample executable.
  3. Run the executable.
  4. See the output results and the corresponding code.