🎓 A1Learner
🔍
0 of 47 lessons completed

PowerShell Special Drives

Special drives expose variables, aliases, and functions as if they were files in a folder. You read them with Get-Item and a drive name followed by a colon.

Read a variable through its drive

Create a variable, then read its Value through the Variable: drive.

$myvar = "hello"
(Get-Item Variable:myvar).Value

Read an alias through its drive

Create an alias, then read its Definition through the Alias: drive.

Set-Alias -Name myalias -Value Get-ChildItem
(Get-Item Alias:myalias).Definition

Peek at a function through its drive

Define a function, then read its Name through the Function: drive.

function Get-Greeting { "hi" }
(Get-Item Function:Get-Greeting).Name

Example

$myvar = "hello"
Set-Alias -Name myalias -Value Get-ChildItem
function Get-Greeting { "hi" }
"Variable value: " + (Get-Item Variable:myvar).Value
"Alias definition: " + (Get-Item Alias:myalias).Definition
"Function name: " + (Get-Item Function:Get-Greeting).Name

Key points

  • Variable: drive: the drive that holds your variables as items.
  • Alias: drive: the drive that holds command aliases as items.
  • Function: drive: the drive that holds your functions as items.
  • Use Get-Item with a drive path, such as Variable:myvar, to read one item.
  • A variable item has a Value, an alias item has a Definition, and a function item has a Name.

In this example, one variable, one alias, and one function are created first. Each is then read back through its own drive, taking the Value, the Definition, and the Name in turn.

Note

these drives exist on every machine, but their contents change with your session.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

How do you read the value of a variable through its drive?