From: 011netservice@gmail.com Date: 2025-06-08 Subject: Windows Command 常用指令 https://svc.011.idv.tw/CodeHelper/Windows/WindowsCommand.txt 歡迎來信交流, 訂購軟體需求. #### Windows Command Reference, 20250608 指令繁雜, 本檔案只列出常用簡短指令. 太長的指令或應用, 則以外部檔案補充: https://svc.011.idv.tw/CodeHelper/Windows/CommandSamples/ PowerShell 指令可參考: https://svc.011.idv.tw/CodeHelper/Windows/PowerShell.txt * 代表常用 > null | 將輸出導向 NULL, 即無輸出. calc|小算盤 certmgr, certmgr.msc|[憑證 - 目前的使用者] certlm, certlm.msc|[憑證 - 本機電腦] charmap|字元對應表 chcp | 查詢預設編碼, 台灣的中文windows 是 950. chcp 65001 | 切換預設編碼為 utf-8, 台灣的中文windows 是 950. chkdsk.exe|Chkdsk磁牒檢查 CHOICE /C YN /N /T 5 /D y | 選擇項目 /C=選擇清單, /N=隱藏選擇清單, /T=等待秒數, /D=預設選擇, /M=顯示訊息 ciadv.msc|索引服務程序 cleanmgr|磁碟垃圾整理 cliconfg|SQL SERVER 客戶端網路實用程序 clipbrd|剪貼簿、剪貼板檢視器 (Windows 10 改為 SnippingTool.exe 剪取工具) * cmd.exe|CMD命令提示字元. cmd 檔案中, 需以 start 執行 .exe 檔案, 才會自動關閉視窗. * compmgmt, compmgmt.msc|電腦管理, 工作排程, 事件檢視, 共用資料夾, 本機使用者和群組, 效能, 裝置管理員, 磁碟管理, 服務與應用程式. conf|啟動 Netmeeting control /name Microsoft.CredentialManager | 管理您的認證, 紀錄了 Line.me, skype, twitter、網路芳鄰、遠端桌面、OneDrive Cached Cendential, WindowsLive, XboxLife..等登入帳號密碼, 可在此刪除 Windows 記憶的帳號密碼. control | 控制台 (Windows 10 Control Pannel) control access.cpl | 協助工具選項 control appwiz.cpl | 程式和功能, 解除安裝或變更程式. Add or Remove Programs. control appwiz.cpl | 新增/移除程式 control desk.cpl | 顯示內容 control firewall.cpl | firewall | 防火牆 Windows Defender Firewall. control fonts | 字型資料夾 control inetcpl.cpl | 網際網路內容 control intl.cpl | 地區設定 control joy.cpl | 搖桿內容 control main.cpl | 滑鼠內容 control main.cpl keyboard | 鍵盤內容 control main.cpl pc card (PCMCIA) | PC 卡 control main.cpl power | 電源管理 (Windows 95) control mlcfg32.cpl | Microsoft Exchange (或 Windows Messaging) control mmsys.cpl | 多媒體內容 control mmsys.cpl sounds | 音效內容 control modem.cpl | 數據機內容 control netcpl.cpl | 網路內容( 注意:在 Windows NT 4.0 中,網路內容是 Ncpa.cpl,不是 Netcpl.cpl control password.cpl | 密碼內容 control powercfg.cpl | 電源管理 (Windows 98) control printers | 印表機資料夾 control sticpl.cpl | 掃描器與數位相機 control sysdm.cpl | 系統內容 control sysdm.cpl add new hardware | 加入新的硬體 control timedate.cpl | 日期/時間內容 control userpasswords | 變更你的使用者帳戶, 新介面, User Accounts. control userpasswords2 | *常用, 使用者帳戶, User Accounts(netplwiz), 選擇(進階使用者管理.進階)可進入[lusrmgr - 本機使用者和群組(本機)]. control wgpocpl.cpl | Microsoft Mail 郵局 dcomcnfg|開啟系統元件服務 ddeshare|開啟DDE共享設定 devmgmt, devmgmt.msc|*常用, 裝置管理員 dfrg.msc|磁碟重組程式 diskmgmt.msc|磁牒管理實用程序 diskpart | 磁碟分割工具, 可刪除修復磁碟區及EFI磁碟區, list disk/select disk n/list partition/select partition n/delete partition noerr override drwtsn32|系統醫生 dvdplay|DVD播放器 dxdiag|檢查DirectX資訊 eudcedit|造字程序 eventvwr, eventvwr.msc|事件檢視器 ????, todo, 20250608 * explorer|檔案總管 * find [string] | 舊版 cmd 可使用 find * findstr [string] | PowerShell 改用 findstr 例如: findstr :25 And netstat -abno | findstr :25 | findstr ESTABLISHED Wildcard, netstat -abno | findstr :25.*EST Or, netstat -abno | findstr "80.*EST | 443.*EST" Not, netstat -abno | findstr "80.*EST | 443.*EST" | findstr /V "UDP" * firewall | *常用, 開啟防火牆. fsmgmt, fsmgmt.msc|共用資料夾管理器 gpedit, gpedit.msc|本機群組原則, 電腦設定.Windows設定.安全性設定.本機原則.安全性選項.互動式登入:不要在登入期間顯示使用者名稱.啟用. inetmgr | 在 執行視窗中 輸入, 可啟動 Internet Information Services (IIS) 管理員 iexpress|木馬元件服務工具,系統原有的 logoff|登出指令 * lusrmgr | lusrmgr.msc | Windows Home 不能使用 [lusrmgr - 本機使用者和群組(本機)]. 可啟用或停用 Administrator 帳號. makecert | 建立 X509 憑證, 需下載 Windows Kits, 檔案位置例如: "C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64" magnify|放大鏡實用程序 mem.exe|顯示記憶體使用情況 mmc|開啟控制台 mobsync|同步指令 mplayer2|簡易媒體播放機Widnows Media Player msconfig.exe|系統配置實用程序 mspaint|小畫家 * mstsc|遠端桌面 narrator|螢幕「講述人」 net start "ServiceName"| 啟動服務 net start w3svc| 啟動 IIS 服務 net stop "ServiceName"| 停止服務 net stop w3svc| 停止 IIS 服務 net use \\computername /delete | 刪除網路芳鄰的連結, 移除帳號密碼. (若已記憶密碼, 則參考 control userpasswords2 指令, 或 啟動 認證管理員後刪除Windows認證 的遠端電腦) net use \\IP /delete | 刪除網路芳鄰IP的連結, 移除帳號密碼. (若已記憶密碼, 則參考 control userpasswords2 指令, 或 啟動 認證管理員後刪除Windows認證 的遠端電腦) net use | 顯示目前連線資訊 net use e: \\192.168.0.14\c$ /delete | 中斷 192.168.0.14 電腦中預設隱藏目錄 c$ 為 e 碟. net use e: \\192.168.0.14\c$ /persistent | 永久連接(重開機) 192.168.0.14 電腦中預設隱藏目錄 c$ 建立為 e 碟. (Windows home 版本不能使用預設隱藏目錄) net use e: \\192.168.0.14\c$ | 連接 192.168.0.14 電腦中預設隱藏目錄 c$ 建立為 e 碟. net user \\192.168.0.35\ShareWork /delete | 刪除網路連結. 例如 \\192.168.0.35\ShareWork. net user administrator /active:no 關閉 windows home 系統管理員帳號. net user administrator /active:yes 啟用 windows home 系統管理員帳號. netmeeting compmgmt.msc---電腦管理 netmeeting dvdplay--------DVD播放器 * netplwiz| 本機使用者和群組(本機), , User Accounts(netplwiz), 選擇(進階使用者管理.進階)可進入[lusrmgr - 本機使用者和群組(本機)]. netsh int ipv4 show excludedportrange protocol=tcp | 列出(防火牆排除的 ports), 導致存取權限不足. Port 10000以下很容易衝突! netsh int ipv4 add excludedportrange protocol=tcp numberofports=1 startport=30001 | 新增自訂(防火牆排除的 ports). Port 10000以下很容易衝突! 可保留30000到49999間的空曠區. netsh int ipv4 delete excludedportrange protocol=tcp numberofports=1 startport=30001 | 刪除自訂(防火牆排除的 ports). netstat | 顯示通訊協定統計資料和目前的 TCP/IP 網路連線. -a=全部, -b=執行檔, -n=數字格式, -o=處理程序識別碼 -t=卸載狀態. netstat find [string] | 舊版 cmd 可使用 find netstat findstr [string] | PowerShell 改用 findstr netstat -an | findstr "LISTEN" | 查詢監聽中的埠. netstat -aon | findstr :4433 | 查詢 監聽4433埠的 PID. 可再利用 PID, 以 tasklist /FI "PID eq [PID]" 查詢程式名稱. netstat -aon | findstr "LISTEN" | 查詢監聽中的埠. 可再利用 PID, 以 tasklist /FI "PID eq [PID]" 查詢程式名稱. netstat -an | findstr "LISTEN" | findstr ":3306" 查詢監聽中的 3306 埠. netstat -an | findstr "EST" | findstr ":80" 查詢連線中的 80 埠. netstat -an | findstr "EST" | findstr /V "127.0.0.1" 查詢連線中外部位址.(不含 127.0.0.1) notepad|開啟記事本 nslookup|IP位址偵測器 ntbackup|系統製作備份和還原 ntmsmgr.msc|移動存儲管理器 ntmsoprq.msc|移動存儲管理員操作請求 odbcad32|ODBC資料來源管理器 oobe /msoobe /a|檢查XP是否啟動 Optimize-VHD -Path c:\test\dynamic.vhdx -Mode Full | PowerShell osk|開啟螢幕小鍵盤 packager|對像包裝程序 perfmon.msc|電腦效能監測程序 powercfg –h off 關閉休眠模式 Hibernate Mode, 執行後須重啟電腦才會刪除hiberfil.sys檔案. (休眠為完全關機, 並存放現狀到hiberfil.sys檔案中, 以便開機時還原. 跟睡眠模式或待機模式 (Sleep/Standby Mode) 不同: 睡眠模式不會完全關機, 只會關閉用不到的裝置, 以低耗電量保存記憶體中的資料, 可以在極短的時間讓電腦喚醒恢復之前的工作狀態 powercfg –h on 開啟休眠模式 Hibernate Mode. progman|程序管理器 regedit.exe|註冊表 regedt32|註冊表編輯器 regsvr32 /u *.dll|停止dll文件執行 regsvr32 /u zipfldr.dll|取消ZIP支持 ROBOCOPY source destination [file [file]...] [options] rononce -p|15秒關機, windows 10 無效. route print -4|檢查 介面清單 和 IPv4 路由表 routing table. rsop.msc|群組原則結果集 sc config "ServiceName" start= auto(demand, disabled) | 設定服務為自動、手動或停用) sc config w3svc start= auto | 設定 IIS服務 為自動. sc delete "ServiceName"| 刪除服務 Deletes a service from the registry. sc start "ServiceName"| 啟動服務 sc stop "ServiceName"| 停止服務 sc.exe | Service Control, SC 是一個用來和服務控制管理員及服務溝通的命令列程式. secpol.msc|*常用, 本機安全性原則 services.msc|*常用, 本機服務. sfc /scannow|Windows文件保護 掃瞄錯誤並復原 sfc.exe|系統檔案檢查器 shrpubw|新增共用資料夾 shutdown.exe /f /l /t 00 | *常用, 登出 logoff c:\WINDOWS\system32\shutdown.exe /f /l /t 00 shutdown.exe /f /r /t 00 | *常用, 重新啟動 reboot c:\WINDOWS\system32\shutdown.exe /f /r /t 00 shutdown.exe /f /s /t 00 | *常用, 關機 shutdown c:\WINDOWS\system32\shutdown.exe /f /s /t 00 sigverif|文件簽名驗證程序 sndrec32|錄音機 sndvol32|音量控制程序 SnippingTool | 剪取工具, 新版的剪貼簿, 按win-shift-s 可剪螢幕畫面. start | 開始另一個視窗來執行指定的程式或命令. cmd 檔案中, 需以 start 執行 .exe 檔案, 才會關閉視窗. syncapp|新增一個公文包 sysedit|系統配置編輯器 syskey|系統加密,一旦加密就不能解開,保護Windows XP系統的雙重密碼 taskkill /f /IM explorer.exe 關閉桌面 taskmgr|*常用, 工作管理員(Windows 2000/XP/2003) * tasklist /FI "PID eq [PID]" | 以 PID 查詢程式名稱. * telnet|純文字終端機, Windows 從 Vista 開始不再預先安裝, 需開啟安裝項目後, 才能使用. timeout /t 5|等待5秒 tourstart|Windows XP簡介(安裝完成後出現的漫遊XP程序) tsshutdn|60秒倒計時關機指令 utilman|協助工具管理器 wiaacmgr|掃瞄儀和照相機嚮導 winchat|Windows XP內建區域網路聊天 winmsd|系統資訊 winver|檢查Windows版本 wmic | WMI Service Command wmic useraccount get name,sid 列出windows使用者清單及SID wmimgmt.msc|開啟Windows管理體系架構(WMI) write|寫字板 WordPad for .rtf file. wscript|Windows指令碼執行器 Cscript為文字模式 wupdmgr|Windows更新程序 xcopy | 複製檔案和目錄, xcopy [SourceDir] [DestinationDir] /C /D /E /F /H /R /Y, /C=錯誤後繼續執行, /D=只複製較新的檔案, /E=包含目錄、子目錄跟空目錄, /F=複製時顯示檔名, /H=保含隱藏及系統檔案, /R=包含唯讀檔案, /Y=不須提示確認. 例如: xcopy d:\pkg\*.* . /C /D /E /F /H /R /Y #### NetStat 狀態說明 [狀態名稱] [說明] [LISTENING] 等待來自遠端的連線請求(伺服器端) [ESTABLISHED] 連線已建立,雙方可以傳送資料 [SYN_SENT] 正在發送連線請求(主動端) [SYN_RECEIVED] 收到連線請求,正在回應(伺服器端) [FIN_WAIT_1] 主動關閉連線的一方,已發送 FIN [FIN_WAIT_2] 等待對方的 FIN [CLOSE_WAIT] 收到對方的 FIN,等待本地應用程式關閉 [CLOSING] 雙方都發送了 FIN,等待確認 [LAST_ACK] 被動關閉方,已發送 FIN,等待 ACK [TIME_WAIT] 等待足夠時間以確保對方收到 ACK(通常 2 倍的最大段壽命) [CLOSED] 連線已完全關閉,沒有任何活動 #### 常用 Port 20, 檔案傳輸協定 - 預設資料埠 21, 檔案傳輸協定 - 控制埠 22, SSH(Secure Shell) - 安全遠端登入協定,用於安全檔案傳輸(SCP、SFTP)及埠轉發. 23, Telnet. 25, SMTP(簡單郵件傳輸協定) - 用於傳遞電子郵件 53, 域名服務系統. 80, Http 443, Https. 1433, SqlServer 1883, MQTT(Message Queuing Telemetry Transport) 3306, MySql 3389, 遠端桌面 4433, Visual SvnServer 自訂. 5389, TeamViewer. 5656, IOT, 4G, 內部. 5658, IOT, LoRa, 內部. 12345, IOT, LoRa, 外部. 12347, IOT, 4G, 外部. Visual SvnServer 預設會監聽 443 或 8443(或自訂的)端口,提供 SVN 倉庫的 Web 存取功能 #### UACRremoteRestrictions 2023-06-08 連線到其他電腦, 例如: \\192.168.0.101\d$, 不能使用時, 可檢查以下項目: □ 連線帳戶必須為本機的 Administrators 群組. □ 關閉連線帳戶的 UAC □ 修改 registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\LocalAccountTokenFilterPolicy, 設定 DWord=1 ref: UAC remote restrictions #### 常用檔案路徑 □ C:\Windows\System32\drivers\etc\hosts □ makecert.exe | 例如 Windows 11 在 C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64 下. Windows 10 在 C:\Program Files (x86)\Windows Kits\10\bin\10.0.15063.0\x64 C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64 □ 清除 Visual Studio 網站專案 Cached Temporary ASP.NET Files. Clear Cache for Temporary ASP.NET Files 碰到 網站怪異的情況時, 先把 Cache 清掉後再重新啟動 Visual Studio, 多半可解決: C:\Users\[yourname]\AppData\Local\Temp\Temporary ASP.NET Files\, 刪掉這一層的( root 跟 vs 2個檔案夾)比較快! 若不能刪除, 則通常是 IIS Express 或是 Visual Studio 仍鎖住檔案開啟中, 只要結束 IIS Express 或是 Visual Studio 直行即可避開. 刪除後, 若重新執行 IIS Express 或是 Visual Studio 網站專案後, 則會自動建立 ( root 跟 vs 2個檔案夾). #### 區域網路檔案共享 2022-01-23 A. 開啟網路分享. 控制台.網路和網際網路.網路和共用中心.進階共用設定. 1. 開啟網路探索. 2. 開啟檔案及印表機共用. 3. 關閉以密碼保護共用. 4. 確認是相同的工作群組, 例如 WORKGROUP, HOMEGROUP. B. 分享資料夾. 檔案總管.選擇目錄.內容.共用.輸入"everyone"群組.新增.共用.(讀取、寫入、或移除). 若是要分享整顆硬碟, 則必須新增"everyone"群組到檔案總管.選擇目錄.內容.安全性中. #### 移除磁碟分割區 (例如: windows 10 的修復磁碟區), 小心不要刪除掉錯誤選擇的分割區: 1)以系統管理員身份啟動--命令提示字元 2)執行 diskpart 3)list disk列出磁碟 4)select disk X來選擇磁碟 5)list partition來列出分割區 6)select partition X 選取修復分割區 7)指令 inactive,把分割區設成非使用中 8)delete partition noerr override 9)exit 離開diskpart #### 清除記憶的帳號密碼 control /name Microsoft.CredentialManager | 管理您的認證, 紀錄了 Line.me, skype, twitter、網路芳鄰、遠端桌面、OneDrive Cached Cendential, WindowsLive, XboxLife..等登入帳號密碼, 可在此刪除 Windows 記憶的帳號密碼. 啟動(控制台.使用者帳戶.管理您的認證)或以指令(control /name Microsoft.CredentialManager)直接啟動, 可(檢視與刪除網站、連線的應用程式及網路的已儲存登入資訊). 網站認證: 紀錄 Line.me, skype, twitter...等登入帳號密碼. Windows認證: 紀錄 網路芳鄰、遠端桌面、OneDrive Cached Cendential, WindowsLive, XboxLife..等登入帳號密碼. #### 啟用 windows home 系統管理員帳號 net user administrator /active:no 關閉 windows home 系統管理員帳號. net user administrator /active:yes 啟用 windows home 系統管理員帳號. #### 開機自動登入 Windows 10 或 Windows 11 可透過 A 或 B 兩種方式設定開機自動登入使用者帳號: A. 可透過操作流程如下, 啟動開機自動登入: 1. Ctrl-R 執行 ms-settings:signinoptions 開啟 設定.帳戶.登入選項畫面 2. 關閉選項:"為了改善安全性,僅在此裝置上允許 Microsoft 帳戶的 Windows Hello 登入". 若找不到 關閉選項"為了改善安全性,僅在此裝置上允許 Microsoft 帳戶的 Windows Hello 登入", 則改用(步驟 B.) 3. 執行 regedit 開啟登錄檔編輯器. 4. 找到 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\PasswordLess\Device\DevicePasswordLessBuildVersion 中的 DevicePasswordLessBuildVersion 值, 將原為 2 改為 0. 5. 執行 netplwiz 開啟使用者帳戶. 6. 選擇要自動登入的帳號, 取消勾選(必須輸入使用者名稱和密碼, 才能使用這台電腦), 按套用按鍵後, 輸入自動登入的帳號與密碼, 重新開機即可. 若輸入自動登入的帳號與密碼錯誤, 並不會檢查是否符合, 只會在重新開機時, 顯示密碼錯誤. B. 也可以透過修改登錄檔方式, 啟動開機自動登入: 7.1 執行 regedit 開啟登錄檔編輯器. 7.2 找到 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon 中的 AutoAdminLogon 填入: 1. AutoAdminLogon 填入: 登入帳戶名稱. DefaultPassword 填入: 登入帳戶密碼. 若找不到這三個項目, 可以手動新增字串值的方式填入. 20220130 舊紀錄方式: Windows 10 Home: 把目前使用者的密碼改成空白, 重新開機後, 就會自動登入目前的使用者. Windows 10 Pro: 以系統管理員執行指令 netplwiz (使用者帳戶), 選擇要自動登入的帳號, 取消勾選(必須輸入使用者名稱和密碼, 才能使用這台電腦), 套用後輸入正確的密碼, 重新開機. #### 登入後自動執行程式 2022-05-19 Windows 10: 1. 開啟(startup資料夾): Windows-執行-輸入"shell:startup"開啟資料夾, 或是 自行開啟C:\Users\[你的使用者帳號]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 資料夾. 2. 把你想要開啟的程式捷徑放到(startup資料夾)中. 3. 或是在(startup資料夾)中建立 AutoStart.cmd 內容如下, 啟動指定位置的程式: @@echo off rem shell:startup rem C:\Users\[你的使用者帳號]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup rem @@echo off rem start C:\path1\program1.exe rem timeout /t 10 rem start C:\path2\program2.exe rem (.exe 程式)啟動方式: start C:\path1\program1.exe rem (Windowns App)啟動方式: start LINE: start C:\path1\program1.exe timeout /t 10 start C:\path2\program2.exe #### Windows Update 失敗 1. 先停掉服務 Windows Update. 2. 刪除更新文件: 2.1 C:\Windows\SoftwareDistribution\DataStore\ 2.2 C:\Windows\SoftwareDistribution\Download\ 3. 重新啟動服務 Windows Update. #### 刪除遠端桌面連線紀錄 regedit 刪除遠端桌面連線紀錄: \HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client\Default 與 C:\Documents and Settings\Administrator\My Documents\Default.rdp #### 中文字型中英對照 中文字型名稱改用英文來顯示,例如: 微軟正黑體:Microsoft JhengHei. 標楷體:DFKai-SB. 細明體:MingLiU. #### 防火牆, 20250505 查詢 port netstat -an | findstr :80, 以 cmd 查詢 port 80 是否開啟. 若沒有結果,代表該埠未開啟或未被使用。 Get-NetTCPConnection -LocalPort 80, 查詢本機 port 80 是否開啟. Test-NetConnection -ComputerName 192.168.0.1 -Port 80, 測試 Remote IP:port 是否開啟. 封鎖 port封鎖 port 開啟 port開啟 port 查詢 IP Address 封鎖 IP Address 開啟 IP Address 以下防火牆內容確認後放到上方 查詢跟 3306 port 有關的防火牆項目. Get-NetFirewallRule | Where-Object { $_.Direction -eq "Inbound" -and $_.Action -eq "Allow" } | findstr "3306" □ Summary ○ Windows 防火牆規則沒有執行項目優先順序, 且封鎖等級大於開放, 因此碰到封鎖的規則就完全封鎖了! ○ 自訂的輸入規則, 可在欄位[名稱](或是欄位[描述]), 加入字首辨識符號, 例如 #. □ 允許遠端連線的IP清單 開啟防火牆 1. 輸入規則.新增規則.規則類型=自訂.動作=僅允許安全連線.其餘皆選預設.名稱=#IPSec. 2. 連線安全規則.新增規則.規則類型=豁免驗證.排除電腦=(允許遠端連線的IP清單).其餘皆選預設.名稱=#IPSec排除電腦. ref: https://kheresy.wordpress.com/2019/01/10/windows-firewall-setting-only-aloow-some-ip-connect/ □ 封鎖對外連線Port清單. 完全不開放遠端連線的Port, 可以用這個方法封鎖. 開啟防火牆 ○ 輸入規則.新增規則.規則類型=連接埠.TCP或UDP.特定本機連接埠=(Port清單).動作=封鎖連線.其餘皆選預設.名稱=#BlockPort. (Port清單)如下, 請依實際安裝或需求設定. △ 1433, SqlServer. △ 3306, MySql. △ 3389, 遠端桌面. △ 4433, SvnServer 自訂 Port, 原為443. △ 5389, TeamViewer. □ 限制遠端IP可連線的(Port, 服務, 或程式) 若要限制遠端IP可連線的(Port, 服務, 或程式), 可使用這個方法. 開啟防火牆 1. 在原有輸入規則: (選擇規則).領域.遠端IP位址=(允許對外連線的IP清單) 2. 新增輸入規則: 新增規則.規則類型=連接埠.TCP或UDP.特定本機連接埠=(Port清單).允許連線.其餘皆選預設.名稱=#Port說明.確認新增後, 再執行步驟1. (允許對外連線的IP清單)通為: 1. 本機子網路, 2. 內部網路, 3. 允許對外連線的IP清單. (選擇規則)清單如下, 請依實際安裝或需求設定. △ 檔案及印表機共用 (回應要求 - ICMPv4-In), 開啟或關閉遠端 ping 功能. (注意不是核心網路診斷 - ICMPv4-In) △ 遠端桌面 △ VisualSVN Server △ Teamviewer Remote Control Service △ 1433, SqlServer. △ 3306, MySql. 以下均為防火牆相關 □ 常用Port: 80, Http 1433, TCP, SQL Server 3306, TCP, MySql 3389, 遠端桌面 5800, VNC 5938, TCP/UDP TeamViewer (OUT 80) ... ref to: Windows-FireWall.txt 啟動防火牆程式 control firewall.cpl control /name Microsoft.WindowsFirewall □ 限制(Port, 程式, 或服務)領域, 只允許可以連線的遠端IP位址, *常用. 新增防火牆輸入規則(例如: # 3389 tcp 遠端桌面)後, 在領域頁籤中, 新增遠端IP位址: 例如 192.168.0.2 - 192.168.0.253, 以及其他預定開放的 IP位址. ----限制外部 IP A. 先新增(連線安全規則) 1. 選取左邊的「連線安全規則」,然後點選右邊的「新增規則」. 2. 規則類型=豁免驗證. 3. 電腦列表中新增允許連線的IP(那些連線電腦不需要驗證?).例如: 192.168.0.1-192.168.0.254 B. 再新增輸入規則BlockAll. 1. 選取左邊的「輸入規則」,然後點選右邊的「新增規則」. 2. 規則類型=自訂. 3. 規則動作=僅允許安全連線. 4. 規則名稱=BlockAll. 若為遠端設定, 則須注意依照以上輸入順序設定, 否則會斷線! ---- 指定(程式與外部IP)可通過防火牆. netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=domain netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=private ---- 新增服務通過防火牆 New command: netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes 開啟服務通過防火牆 New command: netsh advfirewall firewall set rule group="remote desktop" new enable=Yes 對所有的設定檔, 開啟服務通過防火牆 netsh advfirewall firewall set rule group="remote desktop" new enable=Yes profile=domain netsh advfirewall firewall set rule group="remote desktop" new enable=Yes profile=private ---- 開啟連接埠 輸入規則: 新增規則: 規則類型=連接埠 (例如 TCP, 80), 動作=允許連線, 選擇套用的設定檔(網域 或 私人 或 公用 或全選), 名稱及描述. 指令: netsh advfirewall firewall add rule name="Open Port 80" dir=in action=allow protocol=TCP localport=80 ---- ref: https://support.microsoft.com/en-us/help/947709/how-to-use-the-netsh-advfirewall-firewall-context-instead-of-the-netsh 舊版指令為: netsh firewall... (適用 windows 2008 或 vista 以前的電腦) 新版指令為: netsh advfirewall... The netsh advfirewall firewall command-line context is available in Windows Server 2008 and in Windows Vista. This context provides the functionality for controlling Windows Firewall behavior that was provided by the netsh firewall context in earlier Windows operating systems. This context also provides functionality for more precise control of firewall rules. These rules include the following per-profile settings: Domain Private Public The netsh firewall command-line context might be deprecated in a future version of the Windows operating system. We recommend that you use the netsh advfirewall firewall context to control firewall behavior. Note The netsh firewall command line is not recommended for use in Windows Vista. This article describes how to use the netsh advfirewall firewall context instead of the netsh firewall context to control Windows Firewall behavior in Windows Server 2008 and in Windows Vista. --------- Example 1: Enable a program 允許程式通過防火牆 Old command: netsh firewall add allowedprogram C:\MyApp\MyApp.exe "My Application" ENABLE New command: netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes 允許程式通過防火牆, 並限制外部可使用的IP, 及設定檔=網域 Old command: netsh firewall add allowedprogram program=C:\MyApp\MyApp.exe name="My Application" mode=ENABLE scope=CUSTOM addresses=157.60.0.1,172.16.0.0/16,LocalSubnet profile=Domain New command: netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=domain 允許程式通過防火牆, 並限制外部可使用的IP, 及設定檔=全部 Old command: netsh firewall add allowedprogram program=C:\MyApp\MyApp.exe name="My Application" mode=ENABLE scope=CUSTOM addresses=157.60.0.1,172.16.0.0/16,LocalSubnet profile=ALL New command: Run the following commands: netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=domain netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=private --------- Example 2: Enable a port 開啟連接埠 Old command: netsh firewall add portopening TCP 80 "Open Port 80" New command: netsh advfirewall firewall add rule name="Open Port 80" dir=in action=allow protocol=TCP localport=80 --------- Example 3: Delete enabled programs or ports 刪除(可通過防火牆)的程式 Old command: netsh firewall delete allowedprogram C:\MyApp\MyApp.exe New command: netsh advfirewall firewall delete rule name=rule name program="C:\MyApp\MyApp.exe" 刪除(可通過防火牆)的連接埠 Old command: delete portopening protocol=UDP port=500 New command: netsh advfirewall firewall delete rule name=rule name protocol=udp localport=500 --------- Example 4: Configure ICMP settings 設定 ICMP (ping) Old command: netsh firewall set icmpsetting 8 New command: netsh advfirewall firewall add rule name="ICMP Allow incoming V4 echo request" protocol=icmpv4:8,any dir=in action=allow 開啟 ICMP (ping) Old command: netsh firewall set icmpsetting type=ALL mode=enable New command: netsh advfirewall firewall add rule name="All ICMP V4" protocol=icmpv4:any,any dir=in action=allow 關閉 ICMP (ping) Old command: netsh firewall set icmpsetting 13 disable all New command: netsh advfirewall firewall add rule name="Block Type 13 ICMP V4" protocol=icmpv4:13,any dir=in action=block --------- Example 5: Set logging 設定紀錄 Old command: netsh firewall set logging %systemroot%\system32\LogFiles\Firewall\pfirewall.log 4096 ENABLE ENABLE New command: Run the following commands: netsh advfirewall set currentprofile logging filename %systemroot%\system32\LogFiles\Firewall\pfirewall.log netsh advfirewall set currentprofile logging maxfilesize 4096 netsh advfirewall set currentprofile logging droppedconnections enable netsh advfirewall set currentprofile logging allowedconnections enable If you want to set logging for a particular profile, use one of the following options instead of the "currentprofile" option: Domainprofile Privateprofile Publicprofile --------- Example 6: Enable Windows Firewall 開啟關閉防火牆 Old command: netsh firewall set opmode ENABLE New command: netsh advfirewall set currentprofile state on 開啟關閉防火牆 並 開啟例外. Old command: netsh firewall set opmode mode=ENABLE exceptions=enable New command: Run the following commands: Netsh advfirewall set currentprofile state on netsh advfirewall set currentprofile firewallpolicy blockinboundalways,allowoutbound 僅對(domain)設定檔, 開啟關閉防火牆 並 開啟例外. Old command: netsh firewall set opmode mode=enable exceptions=disable profile=domain New command: Run the following commands: Netsh advfirewall set domainprofile state on netsh advfirewall set domainprofile firewallpolicy blockinbound,allowoutbound 對(全部)設定檔, 開啟關閉防火牆 並 開啟例外. Old command: netsh firewall set opmode mode=enable profile=ALL New command: Run the following commands: netsh advfirewall set domainprofile state on netsh advfirewall set privateprofile state on If you want to set the firewall state for a particular profile, use one of the following options instead of the "currentprofile" option: Domainprofile Privateprofile Publicprofile --------- Example 7: Restore policy defaults 重設恢復防火牆原狀 Old command: netsh firewall reset New command: netsh advfirewall reset --------- Example 8: Enable specific services 新增服務通過防火牆 Old command: netsh firewall set service FileAndPrint New command: netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes 開啟服務通過防火牆 Old command: netsh firewall set service RemoteDesktop enable New command: netsh advfirewall firewall set rule group="remote desktop" new enable=Yes 對所有的設定檔, 開啟服務通過防火牆 Old command: netsh firewall set service RemoteDesktop enable profile=ALL New command: Run the following commands: netsh advfirewall firewall set rule group="remote desktop" new enable=Yes profile=domain netsh advfirewall firewall set rule group="remote desktop" new enable=Yes profile=private --------- 參考: 開啟防火牆: netsh advfirewall set currentprofile state on 關閉防火牆: netsh advfirewall set currentprofile state off 以MySQL資料庫為例: 介面阜:3306 放行指定界面阜(大陸叫端口): netsh advfirewall firewall add rule name=“mysql” dir=in action=allow protocol=tcp localport=3306 關閉指定界面阜: netsh advfirewall firewall delete rule name=“mysql” protocol=tcp localport=3306 禁止ping : netsh advfirewall firewall add rule name=”echo-request” protocol=icmpv4:8,any dir=in action=allow 開啟ping: netsh advfirewall firewall del rule name=”echo-request” protocol=icmpv4:any,any dir=in action=allow 匯出防火牆設定: netsh advfirewall export “x:路徑/advfirewall.pol” 導入防火牆設定: netsh advfirewall import “x:路徑/advfirewall.pol” ---- 自動掛接VHD或VHDX 2022-05-03 ref: https://www.nucleustechnologies.com/blog/auto-mount-vhd-vhdx-file-at-startup-in-windows-10/ 建立文字檔 AutoMount.txt 如下 select vdisk file=”C:\Users\poojac\Documents\Outlook Files\Auto-Mount.vhd” attach vdisk select part 1 assign letter=R exit 建立工作排程: Under General tab, Type the Name of the task. Auto-Mount-on-Startup Type in a Description for a better understanding of the task in detail. Mount VHD file on Windows startup automatically. Select Run whether the user is logged on or not and Run with highest privileges. 勾選 Do not store password. The task will only have access the local computer resources. 勾選 Run with highest privileges Under Triggers Tab: Select the New button to set up a New Trigger. Select At Startup for Begin the task. Under Advanced Settings choose delay task for (time). Under Actions Tab: Create a new action by clicking on the New button Select Action as Start a program, select the Program/Script and in the Add arguments(optional) enter: /s (followed by space) and enter the complete path and name of the script file (the text file created in above). Under Conditions Tab: Uncheck the Stop if the computer switches to battery power. Uncheck Start the task only if the computer is on AC power. Click OK to create the task. Step 5: Exit Computer Management and restart your computer to test the auto-mount method. At the End: The method works successfully for all VHD or VHDX files present on your computer or you have created a new one. But, in case any of this VHD file is corrupted or damaged then you will fail to auto-mount it. Therefore, to get rid of the VHD corruption and damage you can try a professional solution specially designed for recovery of VHD files known as Kernel for VHD Recovery software. The intelligent application works flawlessly to repair and restore complete data of VHD/VHDX files within in no time. ---- 2022-05-07, Windows 伺服器安全檢查清單 □ 停用 administrator 帳號或更改 administrator 密碼. ○ 以 [lusrmgr | lusrmgr.msc | netplwiz 本機使用者和群組(本機)], 停用 administrator 帳號或更改 administrator 密碼. ○ 新增並使用一般權限較低的帳號 以 [lusrmgr | lusrmgr.msc | netplwiz 本機使用者和群組(本機)], 新增一般權限較低的帳號, 平常只用這些帳號. □ 允許遠端連線的IP清單 開啟防火牆 1. 輸入規則.新增規則.規則類型=自訂.動作=僅允許安全連線.其餘皆選預設.名稱=#IPSec. 2. 連線安全規則.新增規則.規則類型=豁免驗證.排除電腦=(允許遠端連線的IP清單).其餘皆選預設.名稱=#IPSec排除電腦. ref: https://kheresy.wordpress.com/2019/01/10/windows-firewall-setting-only-aloow-some-ip-connect/ □ 封鎖對外連線Port清單. 完全不開放遠端連線的Port, 可以用這個方法封鎖. 開啟防火牆 ○ 輸入規則.新增規則.規則類型=連接埠.TCP或UDP.特定本機連接埠=(Port清單).動作=封鎖連線.其餘皆選預設.名稱=#BlockPort. (Port清單)如下, 請依實際安裝或需求設定. △ 1433, SqlServer. △ 3306, MySql. △ 3389, 遠端桌面. △ 4433, SvnServer 自訂 Port, 原為443. △ 5389, TeamViewer. □ 限制遠端IP可連線的(Port, 服務, 或程式) 若要限制遠端IP可連線的(Port, 服務, 或程式), 可使用這個方法. 開啟防火牆 1. 在原有輸入規則: (選擇規則).領域.遠端IP位址=(允許對外連線的IP清單) 2. 新增輸入規則: 新增規則.規則類型=連接埠.TCP或UDP.特定本機連接埠=(Port清單).允許連線.其餘皆選預設.名稱=#Port說明.確認新增後, 再執行步驟1. (允許對外連線的IP清單)通為: 1. 本機子網路, 2. 內部網路, 3. 允許對外連線的IP清單. (選擇規則)清單如下, 請依實際安裝或需求設定. △ 遠端桌面 △ VisualSVN Server △ Teamviewer Remote Control Service △ 1433, SqlServer. △ 3306, MySql. △ ping 回應, 檔案及印表機共用 (回應要求 - ICMPv4-In), (注意不是核心網路診斷 - ICMPv4-In) #### 常用Registry - HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers #### PowerShell 2022-01-23, 改以PowerShell-KeyWord.txt存放 本段以下為舊 PowerShell 待搬移. Power Shell Command: 附檔名為 .ps1 執行例: PowerShell(.exe) -File test1.ps1 PowerShell -Command {Get-EventLog -LogName security}, 輸入{}內指令, 不會執行. PowerShell -Command "& {Get-EventLog -LogName security}", 輸入{}內指令後並且執行. PowerShell -Command {dir}, 輸入{}內指令, 不會執行. PowerShell -Command "& {dir}", 輸入{}內指令後並且執行. dir 'C:\', 'fakepath' 2>&1 > .\dir.log | Redirect error stream. Redirect the Error stream to the Success stream, and > to send the resultant Success stream to a file called dir.log Dismount-DiskImage -ImagePath "C:\temp\file.vhd" | 卸載 VHD 或 VHDX. 要先安裝 Hyper-V Module for Windows PowerShell. "hello, world!".ToUpper() | 將"hello, world!"字串轉為英文大寫字元,成為"HELLO, WORLD!" get-childitem | measure-object -property length -sum | 計算一個目錄下檔案內的位元組大小. Get-Help | 指令說明 Get-Help -Online | 線上指令說明 get-process | where { $_.WS -gt 1000MB } | stop-process | 停止所有目前執行中的所有使用大於1000MB記憶體的程式. get-process p* | stop-process | 停止所有目前執行中的以"p"字元開頭命名的程式. Help | 手冊 Mount-DiskImage -ImagePath "C:\temp\file.vhd" | 掛接 VHD 或 VHDX. 要先安裝 Hyper-V Module for Windows PowerShell. netstat -? *>&1 > .\netstathelp.txt | Redirect help. Redirect help information to output file. Optimize-VHD -Path [vhd 或 vhdx 檔案] -Mode Full | PowerShell 壓縮(Compack/Shrink)虛擬磁碟檔案, 清除沒用到的區塊空間. Start-Process ".\App.exe" para1 -WorkingDirectory "C:\WorkDirectory" -WindowStyle Hidden | 啟動背景程式. 在工作管理員中執行 PowerShell 指令, Creating a Scheduled Task which executes a PowerShell script 1. action select: Start a program. 2. Fill out the Start a program form in the following way 2.1 Program/script: PowerShell, 填入"PowerShell" 2.2 Add arguments (optional): .ps1 檔案位置. Path to your PowerShell script including any command line switches 2.3 Start in (optional): 空白 $PROFILE variable A PowerShell profile is a script that runs when PowerShell starts. Current User, Current Host $PROFILE Current User, Current Host $PROFILE.CurrentUserCurrentHost Current User, All Hosts $PROFILE.CurrentUserAllHosts All Users, Current Host $PROFILE.AllUsersCurrentHost All Users, All Hosts $PROFILE.AllUsersAllHosts 建立 $PROFILE if (!(Test-Path -Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force } 編輯 $PROFILE notepad $PROFILE Power Shell Sample: 訂閱一個指定的RSS Feed並顯示它最近8個主題 $rssUrl = "http://blogs.msdn.com/powershell/rss.aspx" $blog = [xml](new-object System.Net.WebClient).DownloadString($rssUrl) $blog.rss.channel.item | select title -first 8