PowerShell 指令掌握指南 (Get-Command, Get-Alias, Get-Member)


  1. 說明
    1. Get-Alias
    2. Get-Command
    3. Get-Member
  2. 應用情境
    1. 取得資料夾安全性
  3. 相關連結

筆記如何使用 PowerShell Get-Command, Get-Alias 以及 Get-Member 熟悉 PowerShell 指令以及物件使用方式。

logo

說明

Get-Alias

取得 Cmdlet 的縮寫,熟記縮寫能夠提升使用 PowerShell 的便利性。

下列是實用的 Alias

Command Alias Usage
Get-Alias gal 取得所有的縮寫
Get-Command gcm 取得所有的指令
Get-ChildItem dir 顯示資料夾及檔案
Get-Content type 印出檔案內容
Get-Item gi 取得檔案資訊
Get-Location pwd 顯示目前路徑
Get-PSDrive gdr 取得磁碟資訊
Get-Module gmo 取得模組資訊
Get-Variable gv 取得變數資訊
Get-Process ps 取得Process資訊
Get-Service gsv 取得Service資訊

Get-Command

取得所有可以執行的 Command,可以在搭配 Get-Alias 或是 Get-Help 確認是否有 Alias 可以使用。
get-alias | ? {$_.Definition -like 'get*'}

get-help get-alias
# gal

Get-Member

取得 Object 所有的 Method 與 Property,認識你的物件,活用你的物件。

應用情境

取得資料夾安全性

直接使用 Out-GridView 進行 Preview

Dir -Directory | Get-ACL | OGV

將資料以 Json 格式匯出,但目前尚不是完美的格式

Dir -Directory `
  | % { GET-ACL $_.Name | SELECT path, access | CONVERTTO-JSON} `
  | OUT-FILE C:\temp\acl.json
{
"Path": "Microsoft.PowerShell.Core\\FileSystem::D:\\Web\\Upload",
"Access": [
    {
        "FileSystemRights": 1245631,
        "AccessControlType": 0,
        "IdentityReference": "BUILTIN\\IIS_IUSRS",
        "IsInherited": false,
        "InheritanceFlags": 3,
        "PropagationFlags": 0
    },
    {
        "FileSystemRights": 1179817,
        "AccessControlType": 0,
        "IdentityReference": "BUILTIN\\IIS_IUSRS",
        "IsInherited": true,
        "InheritanceFlags": 3,
        "PropagationFlags": 0
    }
}

相關連結

PowerShell 常用指令筆記

Powershell 使用物件與資料結構 (Array, Hash)