0% found this document useful (0 votes)
42 views7 pages

About - Automatic - Variables - Microsoft Docs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

About - Automatic - Variables - Microsoft Docs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
‘uesio019 ‘about_Aulomatic Variables | Microsoft Docs Powershell 6~ 3 so 40 30 . . About Automatic Variables 12/21/2017 12 minutes to read Contributors @ DB > PE all Inds aie ‘SHORT DESCRIPTION LONG DESCRIPTION SHORT DESCRIPTION Describes variables that store stat information for PowerShell. These variables are created and maintained by PowerShell LONG DESCRIPTION Conceptually, these variables are considered to be read-only. Even though they can be written to, for backward compatibility they should not be Here isa list of the automatic variables in PowerShell: $8 Contains the last token i the las ine received by the session, 9 Contains the execution status ofthe last operation It contains TRUE ifthe last operation succeeded and FALSE i it failed, $0 Contains the fist token inthe as line received by the session. s Same as. sstter . Contains the current object in the pipeline object. You can use ths variable in commands that perform an action on every abject or on selected objects in a pipeline. SARGS Contains an aray ofthe undeclared parameters and/or parameter values that are passed to a function, script, or script block, When you create a function, you can declare the parameters by using the param keyword or by adding a comma-separated lst of parameters in parentheses after the function name, In an event action the. $irgs_ variable contains objects that represent the event arguments ofthe event that being processed, This variable is populated only within the Action block of an event registration command. The value of this variable can also be found in the SourceArgs property of the PSEventArgs object that Gee-tvent returns SCONSOLEFILENAME Contains the path of the console fle (sc) that was most recently used inthe session. This variable is populated when you start PowerShell withthe PSConsolefile parameter or when you use the export-conssle malt to expart snap-in names toa console file, When you use the Grpart-cansoie cmdlt without parameters it automatically updates the console file that was most recently used in the session You can use this automatic variable to determine which file will be updated, hitpsiidocs.mierosof. com/en-usipowershelimodulemicrosoft.powershel.core/abouabout_automatie_varables?view=powershell-6 wr ‘uesio019 ‘about_Aulomatic Variables | Microsoft Docs SERROR Contains an array of errar objects that represent the most recent errrs. The most recent errr isthe first eror abject in the array. serror( To prevent an errr from being added to the sterar array, use the ErrorAction common parameter with a value of Ignore. For more information, see about CommonParameters, SEVENT Contains a PSEventArgs object that represents the event that i being processed, This variable is populated only within the Action block of an event registration command, such as segsster-cojecrtveat. The value ofthis variable is the same object that the. Yyou can use the properties of the event variable, such a5. event. Tinesenerates, ih an Action script block cvnt ema retuins. Therefore, SEVENTARGS: Contains an object that represents the frst event argument that derives from EventArgs of the event that is being processed, This variable is populated only within the Action block of an event registration command. The value of this variable can also be found inthe SourceEvertAros property of the PSEventArgs object that cet-tvent retutns. SEVENTSUBSCRIBER Contains a PSEventSubscriber object that represents the event subscriber ofthe event that is being processed, This variable is populated only within the Action block of an event registration command, The vale ofthis variable i the same object that the et-tventsubscriber cmdlet relutns SEXECUTIONCONTEXT Contains an Enginelntrinsies object that represents the execution context of the PowerShell host: You can use this variable to find the execution objects that are available to emdlets SFALSE Contains FALSE. You can use ths variable to represent FALSE in commands and scrips instead of using the string “alse”. The string can be interpreted as TRUE itis converted to a non-empty sting or to a non-zero integer. SFOREACH Contains the enumerator (not the resulting values) of a ForEach loop. You can use the properties and methods of enumerators on the value of the sfortach variable, Ths variable exists only while the foreach loop is running: itis deleted after the loop is completed. For detailed information, see about ForFach SHOME Contains the full path ofthe user's home directory. This variable is the equivalent of the “Serw:hanedeivesarv:torepath” Windows environment variables, typically c:\users\eusernane> SHOST Contains an object that represents the current host application for PowerShell. You can use this variable to represent the current hast in commands corto display or change the properties of the host, such as $iort.version OF sHort.currntculture , oF Shost.ul.rakul.setsaceeroundcolor("Red") SINPUT Contains an enumerator that enumerates all input thats passed to a function. The. $input variable is available only to functions and script blocks (which ate unnamed functions) Int the Process block completes, there are no objects leftin the pipeline, so the. sinput_ variable enumerates an empty collection. I the function does rot have a Process block then inthe End block the Siaput_ variable enumerates the collection ofall input to the function. Pracess block of a function, the input variable enumerates the object that is currently inthe pipeline. When $IsCorecLR Contains. srave_if the current session is running on the NET Core Runtime (CoreCLR). Otherwise contains. seaise hitpsildocs.microsof. com/en-usipowershellmaduleimicrosofpowershel,corelabouvabout_automati_variables?vie ar ‘uesio019 ‘about_Aulomatic Variables | Microsoft Docs StsLinux Contains stave ifthe current session is running on a Linux operating system, Otherwise contains. sFaLse SisMacos, Contains sre ifthe current session is running on a MacOS operating system. Otherwise contains. sratse SisWindows Contains rave ifthe current session is running on a Windows operating system, Otherwise contains. raise SLASTEXITCODE Contains the exit code of the last Windows-based program that was run SMATCHES ‘The watches variable works with the natch and -rotmateh operators: When you submit scalarinput tothe -ratch oF -notmaten operator, and either one detects a match, they return a Boolean value and populate the $Matches automatic variable with a hash table of any string values that ‘were matched. For more informat n about the -naten operator, see about comparison operators SMYINVOCATION Contains an information about the current command, such as the name, parameters, parameter values and information about how the command was stated, called, oF “invoked,” such as the name ofthe script that called the current command. stytmecation is populated only for scripts function, and script blocks. You can use the information in the System Management Automation invocationinfo object that styinvecation returns in the current script, such as the path and file name of the script ( sotovecetion.sycenmand.Path ) oF the name of a function ( snytnvocatson.tycomand. ane) to identify the current command. Tiss particularly useful {or finding the name of the current script Beginning in PowerShell 3.0, wyzewocation has the following new properties. Property Description PsscrptRoot Comins the fullpth to the stip that invoked ‘the curent command, The vale ofthis property is populated onl whan the callers sero. sCommandPath Comins the full path nd fle ame of hee et ‘that invoked the cent command, The value ofthis property is populate only when the callers a sero Unlike the sesscriptnoat and sscomamaran automatic variables, the PSScriptRoot and PSCommandPath properties of the shyznvocation ‘automatic variable contain information about the invoke or calling scrit, not the curtent scrip. SNESTEDPROMPTLEVEL Contains the current prompt level. A value of @ indicates the original prompt level. The value i incremented when you enter 3 nested level and decremented when you ext For example, PowerShell presents 9 nested command prompt when you use the siost.tnterkestedranpt_ method. PowerShell alo presents a nested ‘command prompt when you reach a breakpoint in the PowerShell debugger ‘When you enter a nested prompt, PowerShell pauses the current command, saves the execution context, and increments the value of the sxestedPronptiovel_ variable, To create adcitional nested command prompts (up ta 128 levels) orto return to the original command prompt, hitpsiidocs.mierosof. com/en-usipowershelimodulemicrosoft.powershel.core/abouabout_automatie_varables?view=powershell-6 37 ‘uesio019 ‘about_Aulomatic Variables | Microsoft Docs complete the command, or type exit The stestedrrossti value so that itis abways visible vatiable helps you track the prompt level You can create an alternative PowerShell command prompt that includes this SNULL fruit isan automatic variable that contains a NULL or empty value, You can use this variable to represent an absent or undefined value in commands and serits. Powershell teats. suit as an object with a value that is as an explicit placeholder, s0 you can use srul2_to represent an empty value ina series of values, For example, when nuit isincluded in collection, it counted as one ofthe abject. PowerShell Tacory $a = "one", Snull, “whee” $2. count ovtnt Tcory you pipe the snutl_ variable to the Forescn-onject cmdlet, it generates a value for Snell just as it does forthe other objects Poweshe cory sone", Snull, "three" | Forkach-Object { “Hello * + $.) ovtnt Tacory ‘As result, you cannot use srult_ to mean "no parameter value A parameter value of $nuil_ overrides the default parameter value. However, because PowerShell treats the sruit_ variable asa placeholder, you can use it in scripts like 1 following one, which would not work if null mere ignored. Poweshel coor Sealendar = g{srutl, $nuti, “Meeting”, ru22, Soul, “Team Lunch, $nul) Says = "Sunday", "Monday", "Tuesday", "wecnesaay”, "Thursday", Scurrentoay = @ foreach($day in calender) ‘ SetSday -ne $ou2t) ‘Appointnent on $(Sdays[ScurrentDay])= $day > Seunrentoay+ outost cory ‘Anpointent on Friday: Tean lunch sPID Contains the process identifier (PID) ofthe process that's hosting the current PowerShell session, hitpsildocs.microsof. com/en-usipowershellmaduleimicrosofpowershel,corelabouvabout_automati_variables?vie a7 ‘uesio019 ‘about_Aulomatic Variables | Microsoft Docs SPROFILE Contains the fll path ofthe PowerShell profil forthe current user and the current hast application You can use this variable to represent the profile in commands. Fr example, you can use itn a command to determine whether a profile has been crested Powershell coor est-Path SR0FTLE r, you can useit in a command to create a profile: Powel Bacory ten -Iventype file -Path $9SHIME -Force ‘You can also use tina command to open the profile in Notepad: "cory notepad $profitle ‘SPSBOUNDPARAMETERS, Contains a dictionary ofthe parameters that are passed to a script or function and their current values. This variable has a value only ina scone where parameters are declared, such as a script or function. You can use it te display or change the current values of parameters of to pass parameter values to another script or function, For example Powershell Dcoey function Test { paran(sa, $0) # Display the paraneters in dictionary format. SPsaoundbaraneters call the Test function uth $2 and $b. test gPssounaParaneters SPSCMDLET Contains an object that represents the emdlet or advanced function that is being run. You can use the properties and mi ParameterSetName property contains the name ofthe parameter set that is being used, and the ShouldPracess method adds the Whatf and Confirm parameters to the cmdlet dynamically 10d ofthe abject in your cmalet or function code to respond to the concitions of use. For example, the For more information about the s9scnsiet automatic variable, see about Functions Advanced. $PSCOMMANDPATH Contains the fll path and file name ofthe scrip thats being run. Ths variable is valid in all scripts, SPSCULTURE Contains the name of the culture curently in use in the operating system. The culture determines the display format of tems such as numbers, curcency, and dates. Ths isthe value of the System, Globalization. Culturelnfo.CurrentCulture.Name property ofthe system. To get the System Globalization Culturelnfo object forthe system, use the set-culture_ cmdlet. SPSDEBUGCONTEXT \While debugging ths variable contains information about the debugging environment. Otherwise, it contains a NULL value. As a result, you can use itto indicate whether the debugger has control. When populated, it contains a PsDebugContext object that has Breakpoints and Invocationinfor hitpsiidocs.mierosof. com/en-usipowershelimodulemicrosoft.powershel.core/abouabout_automatie_varables?view=powershell-6 57 ‘uesio019 ‘about_Aulomatic Variables | Microsoft Docs properties, The Invocationinfo property has several useful properties including the Location property. The Location property indicates the path of the scrit that is being debugged. SPSHOME Contains the full path ofthe installation directory for PowerShell typically, senv:ainet\systens2 rsnel1\vi.8_ in Windows systems. You can use this variable in the paths of PowerShell ls. For example, the following command searches the conceptual Help tops for the word "variable" Powel cory select-Steing -P rn Variable -Path Spshone\*.txt SPSITEM Same as $_ . Contains the current abject inthe pipeline object. You can use ths variable in commands that perform an action on every abject or on selected objects in a pipeline, SPSSCRIPTROOT Contains the directory from which a script s being run In PowerShell 20, this variable is valid only in script modules (psmt). Beginning in PowerShell 3. itis valid in all script. SPSSENDERINFO Contains information about the user who started the PSSession, including the user identity and the time zone of the originating computer. This, variable is available only in PSSessions. The sesserderte‘o_ variable includes a user-configurable property, ApplicationArguments, which by default, contains only the sesverssontabte {from the originating session, To add data to the ApplicationArguments property, use the ApplicationArguments parameter of the euerssesstonaption cmalet SPSUICULTURE Contains the name of the user interface (UN culture that is currently in use inthe operating system. The Ul culture determines which text strings are used for userinterface elements, such as menus and messages. This isthe value of the System, Globalization Culturelnfo.CurrentUlCulture.Name property of the system. To get the System, Globalization. Culturelnfo object fr the system, use the Get-uscusture cmdle, SPSVERSIONTABLE Contains a read-only hash table that displays details about the version of PowerShell that is running inthe current session, The table includes the following items: Property Description version “The PowerShal version umber Psion “This property has the value of Desktop for Windows Server and Windows cant versions “This peopary has the value of Cor’ for PomerShall runing under Nano Server of Windows 10 Gicommitd “The commit ofthe source les, n Gu, os Description of the operating system that PowerShal sunning on. hitpsildocs.microsof. com/en-usipowershellmaduleimicrosofpowershel,corelabouvabout_automati_variables?vie er ‘uesio019 ‘about_Aulomatic Variables | Microsoft Docs Property Description Phaform Phaform the operating system is running ‘on. The vale on Linux and macOS is Unc See $1stacos and. $rsLirux PsCompatbleVersions Versions of PowerShell that are compatible with the curent version PsRemotingProtocaWerson ‘The version ofthe Powershell emote management protocol Seriszonversion ‘The version ofthe serialization method WsManstackvesion ‘ha version number of the WS-Management tack sPwD Contains a path object that represents the full path of the current directory, REPORTERRORSHOW VARIABLES. ‘The ReportérrarShow variables are defined in PowerShell, but they are not implemented. cot-toriable gets them, but they do not contain valid ata, SREPORTERRORSHOWEXCEPTIONCLASS, ‘+ SREPORTERRORSHOWINNEREXCEPTION ‘+ SREPORTERRORSHOWSOURCE SREPORTERRORSHOWSTACKTRACE SSENDER Contains the object that generated this event, Ths variable is populated only within the Action block of an event registration command, The value of ‘this variable can also be found in the Sender property of the PSEventArgs object that cet-event_ returns. $Shellid Contains the identifier of the current shell $StackTrace Contains a stack trace for the most recent error STHIS Ina script block that defines a script propery or script method, the srmse_ variable refers to the object that i being extended STRUE Contains TRUE. You can use this variable to represent TRUE in commands and scripts. SEE ALSO ‘© about Hash Tables ‘+ about Preference Variables ‘© about Variables hitpsildocs.microsof. com/en-usipowershellmaduleimicrosofpowershel,corelabouvabout_automati_variables?vie 7"

You might also like