Icacls Power Shell Variable Assignment

On By In 1

Set-Variable

Sets the value of a variable. Creates the variable if one with the requested name does not exist.

Syntax

Description

The Set-Variable cmdlet assigns a value to a specified variable or changes the current value. If the variable does not exist, the cmdlet creates it.

Examples

Example 1: Set a variable and get its value

These commands set the value of the desc variable to A description, and then gets the value of the variable.

Example 2: Set a global, read-only variable

This command creates a global, read-only variable that contains all processes on the system, and then it displays all properties of the variable.

The command uses the Set-Variable cmdlet to create the variable. It uses the PassThru parameter to create an object representing the new variable, and it uses the pipeline operator (|) to pass the object to the Format-List cmdlet. It uses the Property parameter of Format-List with a value of all (*) to display all properties of the newly created variable.

The value, "(Get-Process)", is enclosed in parentheses to ensure that it is executed before being stored in the variable. Otherwise, the variable contains the words "Get-Process".

Example 3: Understand public vs. private variables

This command shows how to change the visibility of a variable to Private. This variable can be read and changed by scripts with the required permissions, but it is not visible to the user.

The sample output shows the difference in the behavior of public and private variables.

Required Parameters

-Name

Specifies the variable name.

Type:String[]
Position:1
Default value:None
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False

Optional Parameters

-Confirm

Prompts you for confirmation before running the cmdlet.

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Accept pipeline input:False
Accept wildcard characters:False

-Description

Specifies the description of the variable.

Type:String
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False

-Exclude

Specifies an array of items that this cmdlet excludes from the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as . Wildcards are permitted.

Type:String[]
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False

-Force

Forces the command to run without asking for user confirmation.

By default, you can overwrite a variable, unless the variable has an option value of ReadOnly or Constant. For more information, see the Option parameter.

Type:SwitchParameter
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False

-Include

Specifies an array of items that this cmdlet includes in the operation. The value of this parameter qualifies the Name parameter. Enter a name or name pattern, such as . Wildcards are permitted.

Type:String[]
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False

-Option

Specifies the value of the Options property of the variable.

The acceptable values for this parameter are:

  • None. Sets no options. ("None" is the default.)
  • ReadOnly. Can be deleted. Cannot be not changed, except by using the Force parameter.
  • Constant. Cannot be deleted or changed. Constant is valid only when you are creating a variable. You cannot change the options of an existing variable to Constant.
  • Private. The variable is available only in the current scope.
  • AllScope. The variable is copied to any new scopes that are created.

To see the Options property of all variables in the session, type .

Type:ScopedItemOptions
Parameter Sets:None, ReadOnly, Constant, Private, AllScope, Unspecified
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False

-PassThru

Returns an object representing the item with which you are working. By default, this cmdlet does not generate any output.

Type:SwitchParameter
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False

-Scope

Specifies the scope of the variable.The acceptable values for this parameter are:

  • Global
  • Local
  • Script
  • A number relative to the current scope (0 through the number of scopes, where 0 is the current scope and 1 is its parent).

Local is the default.

For more information, see about_Scopes.

Type:String
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False

-Value

Specifies the value of the variable.

Type:Object
Position:2
Default value:None
Accept pipeline input:True (ByPropertyName, ByValue)
Accept wildcard characters:False

-Visibility

Determines whether the variable is visible outside of the session in which it was created. This parameter is designed for use in scripts and commands that will be delivered to other users. The acceptable values for this parameter are:

  • Public. The variable is visible. (Public is the default.)
  • Private. The variable is not visible.

When a variable is private, it does not appear in lists of variables, such as those returned by Get-Variable, or in displays of the Variable: drive. Commands to read or change the value of a private variable return an error. However, the user can run commands that use a private variable if the commands were written in the session in which the variable was defined.

Type:SessionStateEntryVisibility
Parameter Sets:Public, Private
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Accept pipeline input:False
Accept wildcard characters:False

Inputs

System.Object

You can pipe an object that represents the value of the variable to Set-Variable.

Outputs

None or System.Management.Automation.PSVariable

When you use the PassThru parameter, Set-Variable generates a System.Management.Automation.PSVariable object representing the new or changed variable. Otherwise, this cmdlet does not generate any output.

Related Links

CEG 233: Linux and Windows

BASH and PowerShell Quick Reference

bash PowerShell Description
Scripting Basics
Put a "shebang" at the beginning of the file:
Change permissions on script file to allow execution.
Give the file a ps1 extension. For downloaded scripts, unblock the file under file properties in Windows Explorer.Steps for making scripting files run. In PowerShell, the first time you do scripting, you will need to set the appropriate security settings: run PowerShell as administrator and type .
shell built-in: execute the commands in a file
(or)
Prints String to the screen. In PowerShell, forces the output to the screen instead of being a return value.

(No spaces around =)
Creates a variable $var. In BASH, do not put whitespace around the equals sign, and do not use a $ in the variable assignment.
(or)Add 5 to $var
A comment
Strings
String comparisons. In BASH, be sure the strings litereals are in quotes.
 Get a list of non-static string members
 Get a list of static string members
Removes the specified characters/text from the beginning of the string.
Removes the specified characters/text from the end of the string.  Suppose $fnm == helloThere.txt; then ${fnm%.???} is helloThere
Pattern Matching
print lines matching a pattern
performs string transformations
Booleans and Conditions
Boolean literals
Arithmetic relational operators
True if a string matches a wildcard pattern
True if a string matches a regular expressions
 Used to filter input by a condition. Remember that $_ refers to the current object being tested.
True if $var is null
True if $var is not null (contains one or more characters)
Logical or and and
True if file exists.
True if file does not exist.
True if file is a directory. In PowerShell, if file is not a file variable, be sure to get the file object first with .
 True if file exists and has a size greater than zero.
 True if file1 is newer (according to modification date) than file2
 True if file1 is older (according to modification date) than file2
Control Structures
If statement. In BASH, be sure to leave a space between the condition and the bracket.
If - else if - else statement
Prints numbers 0 through 9.
Prints numbers 0 through 9.
For each loop
Loop controls: continue stops current loop iteration and begins the next; break exits the loop currently being executed.
The name of file without the path. In PowerShell, remember to first get the file object.
The name directory file is in. In PowerShell, remember to first get the file object.
(or)
The number of bytes in file. In PowerShell, remember to first get the file object.
 The last modified time for file. Remember to first get the file object.
(or)
(or)
Store a list of the files in the current working directory in $files. In PowerShell, check out the -exclude flag as well as the Get-ChildItem cmdlet.
Piping, output and error redirection. In BASH, output redirected to is gone. In PowerShell, output redirected to is gone.
function to print the first argument to the screen.
  (or)  Function returns 5, which is printed after the function call. In PowerShell, any output in a function that is not caught is returned. The return statement merely ends the function. The return value of a BASH function is stored in the variable $?.
File Information/Operations
 Listing of files. For bash, learn the options of .
 Listing of files. For PowerShell, learn and
Graphically displays the directory structure of a drive or path.
List the contents of a file on the
List the contents of a file on the , pausing after each page
Creates a directory.
Deletes a folder if it is empty
print working directory
Change the current directory to the one given as argument.
Saves the current directory name on the stack, and then cd's the one given as argument.
Pop off the top-most name on the stack, and then cd to it
Moves or renames files. In PowerShell, check out the -force and -WhatIf flags. In BASH, check out the -f flag.
Copies files and directory trees recursively.
Copies files. In PowerShell, check out the -force and -WhatIf flags. In BASH, check out the -f flag.
Deletes a file. Check out the -r flag. In PowerShell, check out the -force and -WhatIf flags. In BASH, check out the -f flag.
show the contents of each file in sequence
pagination
Remove files
 Link (hard or soft) to an existing file.
 Link (hard or soft) to an existing file. Type to use it in PowerShell
Change file permissions/attributes
Displays or modifies access control lists (ACLs) of files
Change ownership of a file. In PowerShell, multiple steps are necessary
get/set the file mode creation mask; packed vector of bits controlling the initial permissions on a newly created file
Disk space Used. In PowerShell, try
word count, etc.
Octal dump of file content. Almost always used with -x for hexadecimal dump
Translate/substitute characters; useful in improving interoperability
List associations of commands with extensions. Type to use it in PowerShell
Heuristically determine the type of file content
Search for a string in a file's content. For now, learn it without regexp.
Locate a file. By name, etc. For now, learn it without regexp.
 Gives the full path name of a command
 Gives the full path name of a command.  Type to use it in PowerShell
List the differences between two text files
show the differences between two files
get a list of the 10 largest files in the current directory (recursive)
A powerful text editor. For now, learn to edit simple text files with it.
Simple text editors.
A very powerful multi-purpose text editor. For now, learn to edit simple text files with it.
Processes
times commands, etc.
shows current processes
 Get a list of the 5 processes using the most memory
 Get a list of stopped services
 like ps, but with continuous updates
place a STOPped process in the background
bring a backgrounded process to foreground
kills a running program
show lib calls made
show sys calls made
System
show reference pages
set the values of shell variables
get and show the values of shell variables
lists the current environment variables
the search path
WWW/News/Mail browser
transfer files securely to/from a remote machine
remote login securely
who is on the system, and what they are doing
show mounted volumes, etc.

0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *