PowerShell tips for getting a list of variables _powershell

Source: Internet
Author: User

Our requirement is to list all the variables in the script.

This defines a function get-variable:

Copy Code code as follows:

function get-variable
{

$token = $null
$errors = $null

$ast = [System.Management.Automation.Language.Parser]::P arseinput ($psise. CurrentFile.Editor.Text, [ref] $token, [ref] $errors)

# not complete, add variables your want to exclude from the list:
$systemVariables = ' _ ', ' null ', ' Psitem ', ' true ', ' false ', ' args ', ' host '

$null = $ast. FindAll ({$args [0]-is [System.Management.Automation.Language.CommandAst]}, $true)
$token |
Where-object {$_. Kind-eq ' Variable '} |
Select-object-expandproperty Name |
Where-object {$systemVariables-notcontains $_} |
Sort-object-unique
}

The script is loaded into the ISE editor, and then the get-variable is run interactively.

You will get the list of variables for the currently open script.

If you replace the $psise with a variable that contains the script code. CurrentFile.Editor.Text ". You can run this directly outside of the editor, so that we can use Get-content to load any script into the variable, and use this load variable into the code above.

Support 3.0 and beyond

In the development process, often need to use environment variables (such as the current computer name, login username, PATH environment variables, etc.), then how to know in the PowerShell of the environment variables? And how do you get the value of the specified environment variable?

PowerShell through the environment variable provider (environment Provider) gives us access to environment variables. By default, PowerShell creates a drive (named Env) to deal with environment provider. Therefore, we can use the Env this drive to handle the operation related to environment variables.

1. List all environment variables

We can use "Get-childitem env:" To get a list of all the environment variables. The results of the Hongoben operation are as follows:

Copy Code code as follows:

PS c:\users\zhanghong> dir env:

Name Value
----                           -----
AllUsersProfile C:\ProgramData
AppData C:\Users\zhanghong\AppData\Roaming
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles (x86) C:\Program files (x86) \common files
COMPUTERNAME Zhanghong-book
ComSpec C:\Windows\system32\cmd.exe
Homedrive C:
HomePath \users\zhanghong
Java_home D:\JavaDevEnv\jdk1.6.0_16
......

Note that Get-childitem and Dir are a means, the latter being the alias of the former. Hongo likes to be lazy, so he uses dir directly.
All of the environment variables are listed above, and interested friends can familiarize themselves with them so that they are invoked later when they need to use the value of the variable.

2, get the value of environment variables

Syntax: $env:< variable name >

For example, if I want to get the current computer name, the usage is as follows:

Copy Code code as follows:

PS c:\users\zhanghong> $env: ComputerName
Zhanghong-book

Note that the environment variable is also a variable, so you must have a dedicated prefix "$" for the PowerShell variable before "env:".

About PowerShell to get the value of the environment variables, this article on the introduction so much, I hope to help you, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.