====== Windows Powershell ====== * https://powershell.org/ * [[:starship|Starship]] * [[https://github.com/PowerShell/PowerShell/tree/master/docs/learning-powershell|Learning Powershell]] * [[https://github.com/JanDeDobbeleer/oh-my-posh|oh-my-posh]] * [[https://mva.microsoft.com/en-us/training-courses/getting-started-with-powershell-3-0-jump-start-8276?l=r54IrOWy_2304984382|Microsoft PowerShell Tutorial & Training Course – Microsoft Virtual Academy]] * [[https://github.com/fleschutz/PowerShell|fleschutz/PowerShell: Mega collection of 250+ useful cross-platform PowerShell scripts.]] ===== 기본 개인 profile 파일 ===== * [[https://technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspx|Understanding profile]] * ''$PROFILE'' 환경변수 값으로 프로필 파일 생성 New-Item $profile -force -itemtype file * ''$Home\[My ]Documents\WindowsPowerShell\Profile.ps1'' ===== Powershell Core ===== * [[https://blogs.msdn.microsoft.com/powershell/2018/01/10/powershell-core-6-0-generally-available-ga-and-supported/|Powershell Core 6.0 GA Linux / MacOS X Support]] * Powershell Core는 Linux와 Mac 까지 지원한다. ===== sudo ===== * [[windows:sudo|Windows sudo]] ===== Color ls ===== * [[https://github.com/joonro/Get-ChildItemColor|Get-ChildItemColor]] * [[windows:powershell:gallery|PowerShell Gallary]] 로 설치 * ''$Home\[My ]Documents\WindowsPowerShell\Profile.ps1''에 다음 추가하면 ''l'', ''ls''로 명령실행가능. 기존 명령은 ''dir''로 가능 # for colored ls Import-Module Get-ChildItemColor Set-Alias ls Get-ChildItemColor -option AllScope Set-Alias ll Get-ChildItemColorFormatWide -option AllScope ===== 명령창 없이 명령 실행하기 ===== powershell start-process '원하는명령(혹은 .bat)' -WindowStyle Hidden ===== Consoles ===== * [[http://poshconsole.codeplex.com/|PoshConsole]] Modern Powershell console * [[https://blogs.endjin.com/2016/02/improve-your-windows-command-prompt-and-powershell-experience-with-conemu/|Improve your Windows Command Prompt and PowerShell experience with ConEmu | endjin blog]] * https://hodgkins.io/ultimate-powershell-prompt-and-git-setup ===== process 목록 ===== ps *foo* get-process | findstr foo get-process | where ProcessName -like "*foo*" ===== 환경변수 Environment Variable ===== * [[https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-6|about_Environment_Variables | Microsoft Docs]] #값 읽기 $Env: $Env:PATH # 값 지정 $Env: = "" $Env:path = $env:path + ";c:\temp" Set-Item -Path Env:Path -Value ($Env:Path + ";C:\Temp") # 환경변수 목록 Get-ChildItem Env: ===== 기본명령 ===== * 디렉토리 삭제 Remove-Item [dirname] -Force * Symoblic Link # 관리자 권한 필요 New-Item -ItemType SymbolicLink -Name MySymLinkFile.txt -Target $pshome\profile.ps1 New-Item -ItemType SymbolicLink -Path C:\Temp\MySymLinkFile.txt -Value $pshome\profile.ps1 * grep 대체 [[https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-6|Select-String]] # 문자열 filter "Hello","HELLO" | Select-String -Pattern "HELLO" -CaseSensitive # *.xml 파일들에서 문자열 탐색 Select-String -Path "*.xml" -Pattern "the the" # 매칭이 안되는 부분 찾기 Select-String -Path "process.txt" -Pattern "idle, svchost" -NotMatch * rename - [[https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/rename-item?view=powershell-6|Rename-Item]] Rename-Item -Path old_name -NewName new_name [-Force] ===== curl ===== * [[linux:curl|Curl]] 을 사용하려면 ''cinst curl''로 설치하고 * ''$profile'' 파일에 PowerShell에 자체 ''curl'' alias를 지워야 실제 ''curl'' 명령이 실행된다. remove-item alias:curl ===== wget ===== * ''wget'' 명령은 ''Invoke-WebRequest''의 alias 이다. [[https://superuser.com/questions/362152/native-alternative-to-wget-in-windows-powershell|Native alternative to wget in Windows PowerShell?]] wget http://blog.stackexchange.com/ -OutFile out.html ===== Powershell script from remote ===== * [[https://www.thomasmaurer.ch/2021/07/powershell-download-script-or-file-from-github/|PowerShell: Download script or file from GitHub - Thomas Maurer]] Invoke-WebRequest -Uri https://raw.githubusercontent.com/thomasmaurer/demo-cloudshell/master/helloworld.ps1 -OutFile .\helloworld.ps1; .\helloworld.ps1 * [[https://www.youtube.com/watch?v=omm2YUkTkg0|How to run a Powershell script directly from Github private repository through Powershell - YouTube]] : github api 와 access token 으로 원격 스크립트 실행. # Created by Daniel Jean Schmidt [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $Script = Invoke-RestMethod https://api.github.com/repos/Twikki/youtubetest/contents/Download_Files.ps1?access_token=yourtokenhere -Headers @{”Accept”= “application/vnd.github.v3.raw”} Invoke-Expression $Script * [[https://tomtalks.blog/run-powershell-scripts-directly-github-two-lines/|Run PowerShell Scripts Directly from GitHub with Two Lines - Tom Talks]] # script 주소는 올바르게 변경할 것. $ScriptFromGitHub = Invoke-WebRequest https://raw.githubusercontent.com/tomarbuthnot/Run-PowerShell-Directly-From-GitHub/master/Run-FromGitHub-SamplePowerShell.ps1 Invoke-Expression $($ScriptFromGitHub.Content) ===== tail ===== 마지막 1000 줄 보여주면서 계속 출력하기 Get-Content 파일경로 -Wait -Tail 1000 ===== alias ===== # vi 를 vim.exe 실행하는 것으로 alias 걸기 set-alias -name vi -value vim.exe ===== Timezone ===== * [[https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-timezone?view=powershell-6|Set-TimeZone]] Set-TimeZone -Id "Korea Standard Time" =====언어 변경 ===== * 언어팩은 설정을 통해서만 다운로드 가능하다. 명령행으로 현재까지는 불가함. * [[https://docs.microsoft.com/ko-kr/windows-hardware/manufacture/desktop/add-language-packs-to-windows#get-languages|Add languages to Windows images | Microsoft Docs]] 언어팩을 다운로드 하려면 라이센스 가입을 통해 ISO 파일을 다운로드 받아야 함. # 가능할수도? 테스트 필요. Get-WindowsCapability -Online | ? Name -like '*ko*' # 한국어 관련 목록 버전을 정확히 확인해야함. Add-WindowsCapability -Online -Name 'Language.Basic~~~ko-KR~0.0.1.0' Add-WindowsCapability -Online -Name 'Lnaguage.Fonts.Kore~~~und-KORE~0.0.1.0' * [[https://4sysops.com/archives/adding-and-removing-keyboard-languages-with-powershell/|Adding and removing keyboard languages with PowerShell – 4sysops]] * [[https://4sysops.com/archives/local-experience-packs-add-languages-to-windows-10-and-server-2019/|Local Experience Packs: Add languages to Windows 10 and Server 2019 – 4sysops]] * [[https://docs.microsoft.com/en-us/powershell/module/international/set-winuilanguageoverride?view=win10-ps|Set-WinUILanguageOverride]] * [[https://docs.microsoft.com/en-us/powershell/module/international/set-winsystemlocale?view=win10-ps|Set-WinSystemLocale]] Set-WinUILanguageOverride -Language ko-KR Set-WinSystemLocale -SystemLocale ko-KR # 재부팅 후 적용 ===== Location 변경 ===== * [[https://docs.microsoft.com/en-us/powershell/module/international/set-winhomelocation?view=win10-ps|Set-WinHomeLocation]] * [[https://docs.microsoft.com/en-us/powershell/module/international/get-winhomelocation?view=win10-ps|Get-WinHomeLocation]] * [[https://docs.microsoft.com/ko-kr/windows/win32/intl/table-of-geographical-locations?redirectedfrom=MSDN|Table of Geographical Locations - Win32 apps]] 지역 코드값 * 한국 : ''134'' -> HEX ''0x86'' Set-WinHomeLocation -GeoId 0x86 ===== Reboot ===== * https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/restart-computer?view=powershell-7 Restart-Computer ===== Linux PowerShell ===== * [[https://learn.microsoft.com/ko-kr/powershell/scripting/install/installing-powershell-on-linux|Installing PowerShell on Linux]] * [[https://learn.microsoft.com/ko-kr/powershell/scripting/install/install-ubuntu|Ubuntu에 PowerShell 설치]] ===== 참조 ===== * [[https://gist.github.com/jchandra74/5b0c94385175c7a8d1cb39bc5157365e|PowerShell, Cmder / ConEmu, Posh-Git, Oh-My-Posh, Powerline Customization]] * [[https://www.tutorialspoint.com/powershell/index.htm|Powershell Tutorial]]