Skip to content

ทำไมต้อง PowerShell

  • ถ้าใช้ Windows ใช้ Powershell ได้เลย ไม่ต้องติดตั้งเพิ่มเติม

แนะนำให้ใช้ Command Prompt แทน PowerShell แทน เนื่องจากมันดีกว่าและสมัยใหม่กว่า

Usage

ใช้ใน 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 "สวัสดี, $name!"
}

Say-Hello "คุณผู้ใช้"

Customization

starship.rs faviconStarship

Starship => ทำ prompt shell ให้สวยๆ

  1. ติดตั้ง Nerd Font
⚠️ ติดตั้ง Nerd font ด้วย เพื่อใช้ symbol ไม่เพี้ยน
  1. ติดตั้งwww.nerdfonts.com favicon Nerd Font ให้เรียบร้อย

  2. font ที่ติดตั้ง

  1. กำหนดใน Windows Terminal

  1. ติดตั้ง starship
scoop install starship
winget install --id Starship.Starship
  1. ทำให้ powershell เรียกใช้ starship อัตโนมัติ
~/Documents/PowerShell/Microsoft.PowerShell_profile.ps1
powershell
# เรียกใช้ starship ทุกครั้งเมื่อเปิด PowerShell
Invoke-Expression (&starship init powershell)
  1. ใช้งานได้แล้ว

Released under the MIT License