PowerShell 基础
1 PowerShell 基础
PowerShell 可以简单的理解为 cmd 的高级版,cmd 能做的事在 PowerShell 中都能做,但 PowerShell 还能做很多 cmd 不能做的事情。
PowerShell 内置在 Windows 7、Windows Server 2008 R2 及更高版本的 Windows 系统中,同时 PowerShell 是构建在 .NET 平台上的,所有命令传递的都是 .NET 对象。
PowerShell 有如下特点:
- Windows 7 以上的操作系统默认安装
- PowerShell 脚本可以运行在内存中,不需要写入磁盘
- 可以从另一个系统中下载 PowerShell 脚本并执行
- 目前很多工具都是基于 PowerShell 开发的
- 很多安全软件检测不到 PowerShell 的活动
- cmd 通常会被阻止运行,但是 PowerShell 不会
- 可以用来管理活动目录
Windows 操作系统对应的 PowerShell 版本信息:
操作系统 | PowerShell 版本 |
---|---|
windows server 2008 | 1.0 |
windows server 2008 r2、windows 7 | 2.0 |
windows server 2012、windows 8 | 3.0 |
windows server 2012 r2、windows 8.1 | 4.0 |
windows 10 | 5.0 |
windows server 2016 | 5.1 |
可输入 Get-Host 或者 $PSVersionTable 查看 PowerShell 版本:
PS C:\Users\f_carey> Get-Host
Name : ConsoleHost
Version : 5.1.19041.1237
InstanceId : 59a73758-e3b8-4ac7-b098-6a3f1db1d970
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 C:\Users\f_carey> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.19041.1237
PSEdition Desktop
PSCompatibleVersions {
1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1237
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
2 基本概念
2.1 .ps1 文件
ps1 是 PowerShell 的脚本扩展名,一个 PowerShell 脚本文件其实就是一个简单的文本文件。
2.2 执行策略
为了防止恶意脚本在 PowerShell 中被运行,PowerShell 有个执行策略,默认情况下,这个执行策略是受限模式Restricted
。
使用 Get-ExecutionPolicy
命令查看当前执行策略
PS C:\Users\teamssix> Get-ExecutionPolicy
Restricted