Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Windows

วิธีเปิด/ปิดการรันสคริปต์ PowerShell ใน Windows 11/10

สามารถควบคุมการรันสคริปต์ PowerShell (Script Execution) ได้ผ่าน Group Policy Editor หรือ Registry Editor ใช้สำหรับเพิ่มความปลอดภัยหรืออนุญาตให้รันสคริปต์ตามต้องการ

เปิด/ปิด Script Execution ด้วย Group Policy Editor

  1. Win+R พิมพ์ gpedit.msc Enter
  2. ไปที่: Computer Configuration > Administrative Templates > Windows Components > Windows PowerShell
  3. Double-click Turn on Script Execution
  4. เลือก Enabled เพื่อเปิดใช้งาน / Disabled เพื่อปิด
  5. หากเลือก Enabled ให้เลือก Execution Policy จาก Dropdown:
    • Allow only signed scripts - อนุญาตเฉพาะสคริปต์ที่มี Digital Signature
    • Allow local scripts and remote signed scripts (RemoteSigned) - อนุญาตสคริปต์โลคัล และสคริปต์จาก Remote ที่มี Signature
    • Allow all scripts (Unrestricted) - อนุญาตทุกสคริปต์ (เสี่ยงที่สุด)
  6. Apply > OK

เปิด/ปิด Script Execution ด้วย Registry Editor

แนะนำสร้าง System Restore Point และ Backup Registry ก่อน

  1. Win+R พิมพ์ regedit Enter > Yes
  2. ไปที่: HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows
  3. คลิกขวา Windows > New > Key > ชื่อ PowerShell
  4. ใน Key PowerShell สร้าง 2 ค่า:
    • DWORD (32-bit) Value ชื่อ EnableScripts > Value: 1 (Enable) / 0 (Disable)
    • String Value ชื่อ ExecutionPolicy > Value:
      • AllSigned - อนุญาตเฉพาะ Signed scripts
      • RemoteSigned - อนุญาต Local + Remote Signed
      • Unrestricted - อนุญาตทั้งหมด
  5. OK แล้วรีสตาร์ทเครื่อง

หมายเหตุ: การตั้งค่า Execution Policy ผ่าน Registry จะมีผลต่อทุก User บนเครื่อง (Machine-wide) ในขณะที่ Set-ExecutionPolicy ใน PowerShell เองจะเป็นต่อ User/Process