Skip to content

PowerShell เหมาะกับ Windows และดีกว่า command prompt

powershell เป็น shell ที่ใช้ใน Windows สามารถใช้ได้เลย ไม่ต้องติดตั้งเพิ่มเติม

ดีกว่าและสมัยใหม่กว่า command prompt

การใช้งาน

ใช้ใน Windows Terminal

ใช้ใน Wezterm

~/.wezterm.lua
sh
-- config ของผม
local wezterm = require 'wezterm'
wezterm.on("gui-startup", function(cmd)
    local tab, pane, window = wezterm.mux.spawn_window(cmd or {})
    --- pane:split { direction = "Right" }
    window:gui_window():maximize()
end)
return {
    default_cwd = 'D:\\wrikka\\learn-wrikka-com',
    default_prog = {"pwsh"},
    font = wezterm.font("MesloLGL Nerd Font Mono"),
    font_size = 12.0,
    color_scheme = "Gruvbox Dark",
    hide_tab_bar_if_only_one_tab = true,
    default_cursor_style = "SteadyBar",
    enable_scroll_bar = true,
    keys = {
        {key = "t",mods = "CTRL",action = wezterm.action { SpawnTab = "CurrentPaneDomain" }},
        {key = "n",mods = "CTRL",action = wezterm.action { SplitHorizontal = { domain = "CurrentPaneDomain" } }},
        {key = "w",mods = "CTRL",action = wezterm.action { CloseCurrentTab = { confirm = false } }},
        {key = "v",mods = "CTRL",action = wezterm.action { PasteFrom = "Clipboard" }},
        {key = "c",mods = "CTRL|SHIFT",action = wezterm.action { CopyTo = "Clipboard" }},
        {key = "c",mods = "CTRL",action = wezterm.action_callback(function(window, pane)
            if window:get_selection_text_for_pane(pane) == "" then
                window:perform_action(wezterm.action { SendString = "\x03" }, pane)
            else
                window:perform_action(wezterm.action { CopyTo = "Clipboard" }, pane)
            end
        end)},
        {key = "f",mods = "CTRL",action = wezterm.action.Search { CaseInSensitiveString = "CurrentSelectionOrEmptyString" }},
        {key = "a",mods = "CTRL",action = wezterm.action { SendString = "\x01" }}
    }
}
ใช้ใน VS Code

เมื่อกำหนด   "terminal.integrated.defaultProfile.windows": "PowerShell" ใน settings.json => แล้วใน Terminal สามารถกดเลือก Shell ได้

Command

ดูคำสั่งทั้งหมด => พิมพ์ Get-Command

คำสั่งที่ใช้งานบ่อยๆ => พิมพ์ Get-Alias

Alias ที่ใช้งานบ่อย

คำสั่งคำอธิบาย
lsแสดงรายการไฟล์และโฟลเดอร์ในไดเรกทอรีปัจจุบัน
ni example.txtสร้างไฟล์ใหม่ชื่อ "example.txt"
md MyFolderสร้างโฟลเดอร์ใหม่ชื่อ "MyFolder"
rm example.txtลบไฟล์ชื่อ "example.txt"
cp example.txt MyFolder\example.txtคัดลอกไฟล์ "example.txt" ไปยังโฟลเดอร์ "MyFolder"
mv MyFolder\example.txt AnotherFolder\example.txtย้ายไฟล์ "example.txt" จากโฟลเดอร์ "MyFolder" ไปยังโฟลเดอร์ "AnotherFolder"
rni example.txt new_example.txtเปลี่ยนชื่อไฟล์ "example.txt" เป็น "new_example.txt"

Modules

เราสามารถติดตั้ง Modules ต่างๆได้ ค้นหา Module ที่ www.powershellgallery.com faviconPowershell Gallery Packages

ดู Module ที่ติดตั้ง

พิมพ์ Get-InstalledModule

Script

เราสามารถเขียน Script ต่างๆได้ => learn.microsoft.com faviconรายละเอียดเพิ่มเติม

~/Documents/PowerShell/Microsoft.PowerShell_profile.ps1
powershell
# ตัวอย่าง scripe

function Say-Hello {
    param($name)
    Write-Output "Hello, $name!"
}

Say-Hello "User"