[notes outside the mountain – basics of hackers] learning windows PowerShell script (Part 1)


Video address: [hacker foundation] windows PowerShell script learning

Study notes on December 5, 2019

1. $psversiontable: view the version information of PowerShell.
2. PowerShell interface:
  • Start searching for “PowerShell” in the interface, and click “PowerShell. Exe” or “PowerShell ISE”.
  • Enter “PowerShell” in the command interface of CMD.
3. PowerShell shortcut
  • Alt + F7: clear command history, equivalent to CLS command.
  • CTRL + PgUp: page up.
  • CTRL + PgDn: page down.
  • Up and down arrow keys: switch command line and other history.
  • Left / right arrow keys: move the cursor left and right.
  • Ctrl + Home :
  • Ctrl + End :
  • ESC: clear the current command line.
  • Tab: auto fill and select fill command or file name.
  • Enter: executes the current command.
  • CTRL + C: cancel the command being executed.
  • Backspace: removes the command line character from the right.
  • F2: automatically add the history command to the specified character.
  • F4: delete the command line to the right of the cursor.
  • F7: the dialog box displays the command line history.
  • F8: retrieve the command line history containing the specified character.
  • F9: select the command according to the command line and other history number. The history number can be viewed through F7.
4. The pipeline of PowerShell
  • The pipeline of PowerShell is object-based, which is different from the text-based pipeline of Linux shell.
  • Pipeline: the output of the previous command is used as the input of the next command.
  • Redirection:
    • “>” means to output to a file to cover the original contents of the file. If the file does not exist, it will be created automatically.
    • “> >” indicates the end of the content added to a file.
5. Operators for PowerShell
  • Support four arithmetic operations
  • Support numeric conversion
  • Support logic operation
6. Using external commands for PowerShell
  • PowerShell is an extension of CMD. Commands of CMD can be executed in PowerShell.

  • Netstat – ano: view the port information, the same as the CMD command.

  • Ipconfig: view the network configuration information, the same as the CMD command.

  • Route print: print route information, the same as CMD command.

  • Start: open the application.

  • Notepad: open the text editor, the same as the CMD command.

  • Calc: open calculator, same as CMD command.

  • &”Command”: open the external command of PowerShell, which is dedicated to PowerShell.

  • $ env:path : output the content of the path environment variable.

  • $ env:path = $ env:path + “dir_ Path “: temporarily add the path environment variable.

7. Command set of PowerShell
  • Get command: get all the commands of PowerShell.
  • There are three command types of PowerShell: alias, function and cmdlet.
  • Alias: alias.
  • Function: function.
  • Cmdlet: an internal command of PowerShell, named in gerund format.
  • Get help: get help information for the command.
  • Get process: get the process information of the system.
  • Get alias: get the command information corresponding to the alias.
  • Get history: get the history information of commands used by the command line.
  • Get date: get the current date and time information.
8. Alias for PowerShell
  • Get help: get the help information of the command, including the alias information of the command.
  • Get alias: get the command information corresponding to the alias.
  • Get alias – name “LS”: get the power shell command whose alias is “LS”.
  • Get-Alias | where {$_. Definition. Startswitch (“remove”)}: get the alias information of the command starting with “remove”.
  • Get alias | group object definition | sort – descending count: displays commands and their aliases in reverse order according to the number of command aliases.
9. Custom alias
  • Set alias: defines the alias of the command.
    • Set-Alias -name txt -value notepad
    • -Name: Specifies the alias of the command.
    • -Value: Specifies the command that needs to define an alias.
  • del alias:txt: delete the alias “TXT”.
  • Export alias: export alias,Export-Alias demo.ps1
  • Import alias: import alias,Import-Alias demo.ps1If it is not successful, you can use the “- force” parameter to force the import.
10. PowerShell variables
  • PowerShell uses the $symbol to define variables without specifying the variable type.
  • $var_name
  • $var_name = value
  • PowerShell is not case sensitive.
  • Defining a variable to use a special variable name requires curly braces around the variable name.
  • Variable assignments can be aliases, commands, expressions, etc.
  • Multiple variables can be assigned at the same time.
11. Variable operation
  • The values of the two variables are interchanged:

    • $tempt = $var1
      $var_1 = $var_2
      $var_2 = $tempt
    • $var_1,$var_2 = $var_2,$var_1
  • ls variable:: view the currently used variables.

  • ls variable:var_name: View variable $var_ Name. Regular expressions are supported.

  • Test-Path variable:var_name : View variable $var_ Whether name exists or not, returns a Boolean value.

  • del variable:var_name : delete the specified variable.

  • Clear variable: clears the specified variable.

  • Remove variable: removes the specified variable.

12. PowerShell automation variables
  • Automation variables are created and maintained by PowerShell itself. Some variables are read-only and cannot be written.
  • $home: user root path.
  • $PID: the ID of the current interface process.
  • $$: last run command.
  • $? : The status of the last command, which returns a Boolean value.
  • $_ : The current variable.
  • $null: null value, null variable.
13. PowerShell environment variables
  • ls env:: view environment variable information.
  • env:var_name: displays the specified environment variable information and supports regular expressions.
  • env:varname=value: create and add an environment variable.
  • del env:var_name: delete the specified environment variable, support regular expression.
  • [environment]::setenvironmentvariable("PATH","D:\","User"): set the user’s $path variable to take effect permanently.
  • [environment]::getenvironmentvariable("PATH","User"): get the environment variable $path.
14. The execution strategy of PowerShell script
  • PowerShell scripts to.ps1suffix.
  • Get executionpolicy: view the script execution permission of the command line, and return restricted to prohibit.
  • Set-Executionpolicy RemoteSigned: allows the command line to run a PowerShell script.
15. PowerShell script call
  • Switch to desktop: CD desktop
  • & “PowerShell_ script_ Path “: execute the script.
  • . bat script file to call the PowerShell script:powershell "& 'PowerShell_script_path'"