[PowerShell] PowerShell学习脚印
生活随笔
收集整理的這篇文章主要介紹了
[PowerShell] PowerShell学习脚印
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#別名
ac = Add-Content
asnp = Add-PSSnapin
clc = Clear-Content
cli = Clear-Item
clp = Clear-ItemProperty
clv = Clear-Variable
cpi = Copy-Item
cpp = Copy-ItemProperty
cvpa = Convert-Path
diff = Compare-Object
epal = Export-Alias
epcsv = Export-Csv
fc = Format-Custom
fl = Format-List
foreach = ForEach-Object
% = ForEach-Object
ft = Format-Table
fw = Format-Wide
gal = Get-Alias
gc = Get-Content
gci = Get-ChildItem
gcm = Get-Command
gdr = Get-PSDrive
ghy = Get-History
gi = Get-Item
gl = Get-Location
gm = Get-Member
gp = Get-ItemProperty
gps = Get-Process
group = Group-Object
gsv = Get-Service
gsnp = Get-PSSnapin
gu = Get-Unique
gv = Get-Variable
gwmi = Get-WmiObject
iex = Invoke-Expression
ihy = Invoke-History
ii = Invoke-Item
ipal = Import-Alias
ipcsv = Import-Csv
mi = Move-Item
mp = Move-ItemProperty
nal = New-Alias
ndr = New-PSDrive
ni = New-Item
nv = New-Variable
oh = Out-Host
rdr = Remove-PSDrive
ri = Remove-Item
rni = Rename-Item
rnp = Rename-ItemProperty
rp = Remove-ItemProperty
rsnp = Remove-PSSnapin
rv = Remove-Variable
rvpa = Resolve-Path
sal = Set-Alias
sasv = Start-Service
sc = Set-Content
select = Select-Object
si = Set-Item
sl = Set-Location
sleep = Start-Sleep
sort = Sort-Object
sp = Set-ItemProperty
spps = Stop-Process
spsv = Stop-Service
sv = Set-Variable
tee = Tee-Object
where = Where-Object
? = Where-Object
write = Write-Output
cat = Get-Content
cd = Set-Location
clear = Clear-Host
cp = Copy-Item
h = Get-History
history = Get-History
kill = Stop-Process
lp = Out-Printer
ls = Get-ChildItem
mount = New-PSDrive
mv = Move-Item
popd = Pop-Location
ps = Get-Process
pushd = Push-Location
pwd = Get-Location
r = Invoke-History
rm = Remove-Item
rmdir = Remove-Item
echo = Write-Output
cls = Clear-Host
chdir = Set-Location
copy = Copy-Item
del = Remove-Item
dir = Get-ChildItem
erase = Remove-Item
move = Move-Item
rd = Remove-Item
ren = Rename-Item
set = Set-Variable
type = Get-Content#PSDrive
alias
cert
env
function
hkcu
hklm
variable#獲取幫助
help get*
help *process
help dir
help dir -full
help dir -detailed
help dir -example
dir -?#轉義序列`0 //空值`a //Beep`b //退格`f //換頁`n //新行`r //回車`t //制表符`v //垂直引號`` // "`"#數字常量1kb // 10241mb1gb1e3 // 10000xFFFF // 65535#編輯腳本,推薦使用PrimalScript的最新版本,非常強大#設置腳本安全策略
set-executionpolicy Restricted #默認
set-executionpolicy AllSigned #部署
set-executionpolicy RemoteSigned #開發
set-executionpolicy Unrestricted #不推薦#執行腳本必須帶路徑
./myScript.ps1#獲取基于用戶名和密碼的憑據對象
$cert = get-credential#對腳本簽名
$cert = Get-PfxCertificate C:\Test\Mysign.pfx
Set-AuthenticodeSignature myScript.ps1 -cert $cert#操作系統
gwmi win32_operatingsystem#自動變量
$Args #傳遞進函數的參數
$_ #通過管道傳入的對象
$input #通過管道傳入的對象集合
$$ #前一命令行的最后一個標記
$? #上一命令的布爾狀態
$^ #前一命令行的第一個標記
$Matches #使用 –match 運算符找到的匹配項的哈希表
$Error[0] #前一次錯誤
$Home #用戶的主目錄
$Host #引用宿主 POWERSHELL 語言的應用程序
$LastExitCode #上一程序或腳本的退出代碼
$PSHome #Windows PowerShell 的安裝位置
$profile #標準配置文件(可能不存在)
$StackTrace #Windows PowerShell 捕獲的上一異常#類型空類型[void] 數值類型[byte] typeof(byte) [decimal] typeof(decimal) [double] typeof(double) [float] typeof(float) [int] typeof(int) [long] typeof(long) [single] typeof(float) 字符類型[char] typeof(char) [string] typeof(string) 布爾類型[bool] typeof(bool) 集合類型[array] typeof(System.Array) typeof(System.Object[]) [hashtable] typeof(System.Collections.Hashtable) 其它[psobject] typeof(System.Management.Automation.PSObject) [ref] typeof(System.Management.Automation.PSReference) [regex] typeof(System.Text.RegularExpressions.Regex) [scriptblock] typeof(System.Management.Automation.ScriptBlock) [switch] typeof(System.Management.Automation.SwitchParameter) [type] typeof(System.Type) [wmi] typeof(System.Management.ManagementObject) [wmiclass] typeof(System.Management.ManagementClass) [wmisearcher] typeof(System.Management.ManagementObjectSearcher) [xml] typeof(System.Xml.XmlDocument)example:
#[void]
[void] $var #可以阻止$var輸出#[xml]
$var = [xml] "onetwo3"
$var.top
$var.top.a
$var.top.a = "13"#自定義函數
function writeln([string] $str) { echo $str }
function writeln([string] $str) { Begin {echo "Begin"} Process {echo $str} End {echo "End"} }
function writeln { param ([string] $str = $(throw "missing parameter")); echo $str }
${function:writeln} = { param ([string] $str = $(throw "missing parameter")); echo $str }#文本文件讀寫
${C:\test.txt} = "Hello`r`n"
${C:\test.txt} += "-----------------"
$Line1 = ${C:\test.txt}[0]# $null
$var = $null #刪除$var
dir > $null
$null = dir#Invoke
$method = "ToUpper"
"abc".$method.Invoke()#布爾值
$true
$false#where, select等的用法
cd c:\
dir | where {$_.Name -eq "WINDOWS"} | select Length,Name
dir | ? {$_.Name -eq "WINDOWS"}# 列出字符串對象"str"的所有屬性與方法
"str" | gm#強制類型變量
[Boolean] $condition = 0
[string] $str = "hello"#數組
$array = 1,2,3,4,5,6,7,8
$array = @(1,2,3,4,5,6,7,8)
$array[-1] #最后一個元素
$array[0..4]
$array[-1..-8] #倒序#哈希表
$hash = @{"Name"="Icebird"; "Job"="Engineer"}#如何取得文本的行數
(type C:\test.txt | measure-object).Count#從控制臺讀取一行輸入
$var = Read-Host#獲取環境變量
$path = $env:Path#讀取注冊表
$a = (gp "hklm:\\Software\Microsoft\Windows\CurrentVersion").ProductId#比較和運算
-like "file.doc" -like "file.*"
-notlike
-contains 1,2,3 -contains 1
-notcontains
----------------------------------
-eq = (忽略大小寫)
-ieq = (忽略大小寫)
-ceq = (不忽略大小寫)
-ne !=
-gt >
-lt <
-ge >=
-le <=
以上操作符也可用于對數組操作:1,2,3,5,3,2 -lt 3
----------------------------------
-and
-or
-not
! 等價于-not
-xor
% 模運算
* 可用于字符串重復: "-" * 80#位操作
-band
-bor
-bnot
-bxor#特殊操作
$a = "Hat"
$a -replace "a","o"
$a -is [string]
$a = 1 -as [string]
$b = @(1..5)
"{0:d}" -f (get-date)
"{0:yyyy-MM-dd}" -f (get-date)#格式化
$a = 123456789.456789
$a.ToString("F4")
"{0:F4}" -f $a
"{0,-20:F4}{1}" -f $a,0
"{0,20:F4}" -f $a
"{0:x}" -f 100000
"{0:X}" -f 100000#正則表達式
$regex = [regex]"He"
$str = "Hello, Hello"
$isMatch = $str -match $regex
$isMatch = $regex.ismatch($str)
$matches = $regex.matches($str)#雙引號與單引號
$a = 1;
"$a"會輸出1
'$a'會輸出$a# & 與 .
.{$var = 1} #看作include
&{$var = 1} #看作call
&"dir" #意味著函數名之類的可以當作參數傳入#foreach
foreach ($file in dir) { $file.Name }
dir | % { $_.Name }#for
for ($i = 1; $i -lt 5; $i++) {echo $i}#while
$i = 0
while ($i -lt 4) { $i++; $i }#do...while
$i = 10
do {$i--; $i} while ($i -lt 5)#do...until
$i = 10
do {$i--; $i} until ($i -lt 5)#if...else[if]
$obj = "Hello"
if ($obj -is [string]) { "ISSTRING" }
if ($obj -is [string]) { "ISSTRING" } else { "ISNOTSTRING" }
if ($obj -is [string]) { "ISSTRING" } elseif ($obj -is [int]) { "ISINTEGER" }#switch
switch (1,2,3) { 1 {"a"} 2 {"b"} 3 {"c"} default {"?"} }
switch (1) { "a" {"China"} "b" {"Japan"} "c" {"c"} default {"???"} }#switch -regex
$var = "abcdefg"
switch -regex($var) {
"^\w+$" {echo $_" is a word"}
"^\d+$" {echo $_" is a number"}
"^\s+$" {echo $_" is a space"}
default {echo "?"}
}#switch -casesensitive
#switch -wildcard
#switch -extract ?怎么用
#switch -file ?怎么用#拋出錯誤&捕獲異常
throw "Error"
raised "Exception"trap
{write-host "Error: " $_.Exception.Message
#$_.TargetObject
#$_.CategoryInfo
#$_.FullyQualifiedErrorID
#$_.ErrorDetails
#$_.InvocationInfocontinue
#break
#return
}#輸出格式化
Format-List
Format-Table
Format-Wide
Format-Customdir | format-table -groupby Mode
dir | sort Name -desc
dir | sort Name -desc | select Name,Length,Mode | export-csv C:\dir.csv
dir | sort Name -desc | select Name,Length,Mode | export-clixml C:\dir.csv
dir | convertto-html#使用.NET的對象
$webclient = New-Object System.Net.WebClient
$webclient.Encoding = [System.Text.Encoding]::UTF8
$url = "http://www.cnblogs.com/rss"
$data = [string]$webclient.downloadstring($url)#使用COM對象
$spVoice = new-object -com "SAPI.spvoice"
$spVoice.Speak("Hello, I am Icebird.")
轉載于:https://www.cnblogs.com/Icebird/archive/2008/02/11/PowerShellStudy.html
總結
以上是生活随笔為你收集整理的[PowerShell] PowerShell学习脚印的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《C陷阱与缺陷》和《C专家编程》两本书又
- 下一篇: 恭喜发财 利市逗来