fbpx Windows | HostingUltraso.com
24/7/365 Support

Windows

Interact with PowerShell’s Global Environment

Problem

You want to store information in the PowerShell environment so that other scripts have access to it.

Solution

To make a variable available to the entire PowerShell session, use a $GLOBAL: prefix when you store information in that variable:

## Create the web service cache, if it doesn't already exist

if(not (TestPath Variable:\Lee.Holmes.WebServiceCache))

{

${GLOBAL:Lee.Holmes.WebServiceCache} = @{}

}

Safely Build File Paths Out of Their Components

Problem

You want to build a new path out of a combination of subpaths.

Solution

To join elements of a path together, use the JoinPath cmdlet:

PS >JoinPath (GetLocation) newfile.txt C:\temp\newfile.txt

Discussion

The usual way to create new paths is by combining strings for each component, placing a path separator between them:

PS >"$(GetLocation)\newfile.txt" C:\temp\newfile.txt

Unfortunately, this approach suffers from a handful of problems:

Get the Current Location in Windows PowerShell

Problem

You want to determine the current location.

Solution

To determine the current location, use the GetLocation cmdlet: PS >GetLocation

Path

C:\temp PS >$currentLocation = (GetLocation).Path PS >$currentLocation C:\temp

Discussion

One problem that sometimes impacts scripts that work with the .NET Framework is that PowerShell’s concept of “current location” isn’t always the same as the PowerShell.exe process’s “current directory.” Take, for example:

PS >GetLocation

Path

C:\temp

Find the Location of Common System Paths in PowerShell

Problem

You want to know the location of common system paths and special folders, such as My Documents and Program Files.

Solution

To determine the location of common system paths and special folders, use the [Environment]::GetFolderPath() method: PS >[Environment]::GetFolderPath("System") C:\WINDOWS\system32

For paths not supported by this method (such as All Users Start Menu), use the WScript.Shell COM object: $shell = NewObject Com WScript.Shell $allStartMenu = $shell.SpecialFolders.Item("AllUsersStartMenu")

Discussion

Find Your Script’s Location in PowerShell

Problem

You want to know the location of the currently running script.

Solution

To determine the location of the currently executing script, use this function:

function GetScriptPath

{

SplitPath $myInvocation.ScriptName

}

Discussion

Once we know the full path to a script, the SplitPath cmdlet makes it easy to determine its location. Its sibling, the JoinPath cmdlet, makes it easy to form new paths from their components as well.

Find Your Script’s Name in PowerShell

Problem

You want to know the name of the currently running script.

Solution

To determine the full path and filename of the currently executing script, use this function:

function GetScriptName

{

$myInvocation.ScriptName

}

To determine the name that the user actually typed to invoke your script (for example, in a “Usage” message), use the $myInvocation.InvocationName variable.

Discussion

Access Information About Your Command’s Invocation

Problem

You want to learn about how the user invoked your script, function, or script block.

Solution

To access information about how the user invoked your command, use the $myInvocation variable:

"You invoked this script by typing: " + $myInvocation.Line

Discussion

The $myInvocation variable provides a great deal of information about the current script, function, or script block—and the context in which it was invoked:

MyCommand

Information about the command (script, function, or script block) itself.

ScriptLineNumber

View and Modify Environment Variables in PowerShell

Problem

You want to interact with your system’s environment variables.

Solution

To interact with environment variables, access them in almost the same way that you access regular PowerShell variables. The only difference is that you place env: between the ($) dollar sign and the variable name:

PS >$env:Username Lee

You can modify environment variables this way, too. For example, to temporarily add the current directory to the path:

Pages

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Processor Intel Atom C2350 1.7 GHz 2c/2t RAM 4 GB DDR3 Storage 1× 1 TB (HDD SATA) Server Detail Configure Server
Processor Intel Atom C2350 1.7 GHz 2c/2t RAM 4 GB DDR3 Storage 1× 128 GB (SSD SATA) Server Detail Configure Server
Processor Intel Atom C2750 2.4 GHz 8c/8t RAM 8 GB DDR3 Storage 1× 1 TB (HDD SATA) Server Detail Configure Server
Processor Intel Xeon E3-1230 v2 3.3 GHz 4c/8t RAM 16 GB DDR3 Storage 1× 256 GB (SSD SATA) Server Detail Configure Server
Processor Intel Atom C2350 1.7 GHz 2c/2t RAM 4 GB DDR3 Storage 1× 250 GB (SSD SATA) Server Detail Configure Server

What Our Clients Say