├── .gitattributes ├── .github └── workflows │ └── dotnet.yml ├── .gitignore ├── LICENSE ├── PSCH.sln ├── PSCH ├── Data │ └── DataContext.cs ├── Model │ └── FavouriteCommand.cs ├── PSCH.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── Services │ └── ChatGptService.cs └── psch.db └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.github/workflows/dotnet.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a .NET project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net 3 | 4 | name: Build 5 | 6 | on: 7 | push: 8 | branches: [ "master" ] 9 | pull_request: 10 | branches: [ "master" ] 11 | 12 | jobs: 13 | build: 14 | runs-on: windows-latest 15 | steps: 16 | - uses: actions/checkout@v4 17 | - name: Setup .NET 18 | uses: actions/setup-dotnet@v4 19 | with: 20 | dotnet-version: 8.0.x 21 | - name: Restore dependencies 22 | run: dotnet restore 23 | - name: Add msbuild to PATH 24 | uses: microsoft/setup-msbuild@v2 25 | - name: Build 26 | run: msbuild /p:Configuration=Release 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 MaciejTrudnos 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /PSCH.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.1.32407.343 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSCH", "PSCH\PSCH.csproj", "{32F99D5E-C6C1-4602-8F74-B047A4DAD7EE}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {32F99D5E-C6C1-4602-8F74-B047A4DAD7EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {32F99D5E-C6C1-4602-8F74-B047A4DAD7EE}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {32F99D5E-C6C1-4602-8F74-B047A4DAD7EE}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {32F99D5E-C6C1-4602-8F74-B047A4DAD7EE}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {E298BEDD-D18C-48F7-8DAE-07E1541DEA75} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /PSCH/Data/DataContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using PSCH.Model; 3 | namespace PSCH.Data 4 | { 5 | public class DataContext : DbContext 6 | { 7 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 8 | { 9 | var dataSource = AppDomain 10 | .CurrentDomain 11 | .BaseDirectory; 12 | 13 | optionsBuilder.UseSqlite(@$"DataSource={dataSource}\psch.db;"); 14 | } 15 | 16 | public DbSet FavouriteCommand { get; set; } 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /PSCH/Model/FavouriteCommand.cs: -------------------------------------------------------------------------------- 1 | namespace PSCH.Model 2 | { 3 | public class FavouriteCommand 4 | { 5 | public int Id { get; set; } 6 | public string Command { get; set; } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PSCH/PSCH.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | disable 8 | MaciejTrudnos 9 | 10 | README.md 11 | https://github.com/MaciejTrudnos/PowerShell-Command-History 12 | mtdeveloper.com 13 | PowerShell-Command-History 14 | Interactive PowerShell command history list 15 | 1.2.0.1 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | tlbimp 25 | 4 26 | 2 27 | 215d64d2-031c-33c7-96e3-61794cd1ee61 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Windows.Forms.dll 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | PreserveNewest 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /PSCH/Program.cs: -------------------------------------------------------------------------------- 1 | using OpenAI.ObjectModels; 2 | using OpenAI.ObjectModels.RequestModels; 3 | using PSCH.Data; 4 | using PSCH.Model; 5 | using PSCH.Services; 6 | using Sharprompt; 7 | using System.Reflection; 8 | using System.Windows.Forms; 9 | 10 | class Program 11 | { 12 | private readonly static string _filePath = Environment.ExpandEnvironmentVariables(@"%userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt"); 13 | private readonly static DataContext _dbContext = new(); 14 | private readonly static List _messages = 15 | [ 16 | ChatMessage.FromSystem("You are a helpful assistant."), 17 | ChatMessage.FromSystem("Return only powershell command in one line.") 18 | ]; 19 | 20 | [STAThread] 21 | private static void Main(string[] args) 22 | { 23 | if (args.Length == 0) 24 | { 25 | if (!File.Exists(_filePath)) 26 | throw new FileNotFoundException($"{_filePath}"); 27 | 28 | var psch = GetPowerShellCommandHistory(); 29 | 30 | var selectedCommand = SelectCommand("Filter:", psch); 31 | 32 | Clipboard.SetText(selectedCommand); 33 | 34 | ClearPSCH(psch); 35 | 36 | return; 37 | } 38 | 39 | if (args[0] == "-gpt") 40 | { 41 | var apiKey = Environment 42 | .GetEnvironmentVariable("OPEN_AI_API_KEY"); 43 | 44 | if (string.IsNullOrEmpty(apiKey)) 45 | throw new ArgumentException("OPEN_AI_API_KEY Environment variable value not found"); 46 | 47 | var model = Models.Gpt_4o; 48 | 49 | if (args.Length == 2) 50 | model = args[1]; 51 | 52 | var gpt = new ChatGptService(apiKey, model); 53 | 54 | var stopChat = false; 55 | int chatCounter = 0; 56 | 57 | do 58 | { 59 | if (chatCounter > 100) 60 | throw new IndexOutOfRangeException("Too Many Requests"); 61 | 62 | var message = Prompt.Input("", placeholder: "Send a message..."); 63 | 64 | _messages.Add(ChatMessage.FromUser(message)); 65 | 66 | var request = gpt.SendMessage(_messages); 67 | 68 | request.Wait(); 69 | 70 | _messages.Add(ChatMessage.FromAssistant(request.Result)); 71 | 72 | Console.ForegroundColor = ConsoleColor.Green; 73 | Console.WriteLine(request.Result); 74 | Console.ResetColor(); 75 | 76 | stopChat = Prompt.Confirm("Close conversation and copy answer"); 77 | 78 | if (stopChat) 79 | Clipboard.SetText(request.Result); 80 | 81 | chatCounter++; 82 | 83 | } while (!stopChat); 84 | 85 | return; 86 | } 87 | 88 | if (args[0] == "-p") 89 | { 90 | if (!File.Exists(_filePath)) 91 | throw new FileNotFoundException($"{_filePath}"); 92 | 93 | var pageSize = int.Parse(args[1]); 94 | 95 | var psch = GetPowerShellCommandHistory(); 96 | 97 | var selectedCommand = SelectCommand("Filter:", psch, pageSize); 98 | 99 | Clipboard.SetText(selectedCommand); 100 | 101 | ClearPSCH(psch); 102 | 103 | return; 104 | } 105 | 106 | if (args[0] == "-v") 107 | { 108 | var assembly = Assembly.GetExecutingAssembly(); 109 | var version = assembly.GetName().Version; 110 | 111 | Console.WriteLine($"PowerShell Command History {version}"); 112 | 113 | return; 114 | } 115 | 116 | if (args[0] == "-h") 117 | { 118 | Console.WriteLine("Usage: psch [OPTIONS] COMMAND"); 119 | Console.WriteLine("Options:"); 120 | Console.WriteLine("-h Print help and quit"); 121 | Console.WriteLine("-v Print version information and quit"); 122 | Console.WriteLine("-p Set page size"); 123 | Console.WriteLine("-s Save command"); 124 | Console.WriteLine("-r Remove command"); 125 | Console.WriteLine("-f Saved commands list"); 126 | 127 | return; 128 | } 129 | 130 | if (args[0] == "-s") 131 | { 132 | if (args.Length > 2) 133 | { 134 | Console.WriteLine("Use quote to save multi args command"); 135 | Console.WriteLine("E.g. psch -s \"your multi args command\""); 136 | 137 | return; 138 | } 139 | 140 | var cmd = new FavouriteCommand 141 | { 142 | Command = args[1] 143 | }; 144 | 145 | _dbContext 146 | .FavouriteCommand 147 | .Add(cmd); 148 | 149 | _dbContext 150 | .SaveChanges(); 151 | 152 | return; 153 | } 154 | 155 | if (args[0] == "-r") 156 | { 157 | var fCommands = _dbContext 158 | .FavouriteCommand 159 | .Select(s => s.Command) 160 | .ToList(); 161 | 162 | var selectCommand = SelectCommand("Remove:", fCommands); 163 | 164 | var favouriteCommand = _dbContext 165 | .FavouriteCommand 166 | .Where(x => x.Command == selectCommand) 167 | .First(); 168 | 169 | _dbContext 170 | .Remove(favouriteCommand); 171 | 172 | _dbContext 173 | .SaveChanges(); 174 | 175 | return; 176 | } 177 | 178 | if (args[0] == "-f") 179 | { 180 | var fCommands = _dbContext 181 | .FavouriteCommand 182 | .Select(s => s.Command) 183 | .ToList(); 184 | 185 | var selectedCommand = SelectCommand("Filter:", fCommands); 186 | 187 | Clipboard.SetText(selectedCommand); 188 | 189 | return; 190 | } 191 | 192 | throw new ArgumentException("Unknown option \nTry 'psch -h' for more information."); 193 | } 194 | 195 | private static List GetPowerShellCommandHistory() 196 | { 197 | return File 198 | .ReadLines(_filePath) 199 | .Where(x => !x.StartsWith("psch", StringComparison.InvariantCultureIgnoreCase)) 200 | .Distinct() 201 | .Reverse() 202 | .ToList(); 203 | } 204 | 205 | private static string SelectCommand(string fallbackValue, List commands, int pageSize = 10) 206 | { 207 | Prompt.Symbols.Prompt = new Symbol("", fallbackValue); 208 | Prompt.ColorSchema.PromptSymbol = ConsoleColor.Gray; 209 | 210 | return Prompt 211 | .Select(string.Empty, commands, pageSize); 212 | } 213 | 214 | static void ClearPSCH(List commandHistory) 215 | { 216 | File.Delete(_filePath); 217 | 218 | using StreamWriter sw = File.AppendText(_filePath); 219 | commandHistory.ForEach(x => sw.WriteLine(x)); 220 | } 221 | } -------------------------------------------------------------------------------- /PSCH/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "PSCH": { 4 | "commandName": "Project" 5 | } 6 | } 7 | } -------------------------------------------------------------------------------- /PSCH/Services/ChatGptService.cs: -------------------------------------------------------------------------------- 1 | using OpenAI; 2 | using OpenAI.Managers; 3 | using OpenAI.ObjectModels; 4 | using OpenAI.ObjectModels.RequestModels; 5 | 6 | namespace PSCH.Services 7 | { 8 | public class ChatGptService 9 | { 10 | private readonly OpenAIService _openAiService; 11 | private readonly string _model; 12 | 13 | public ChatGptService(string apiKey, string model) 14 | { 15 | _openAiService = new OpenAIService(new OpenAiOptions() 16 | { 17 | ApiKey = apiKey 18 | }); 19 | 20 | _model = model; 21 | } 22 | 23 | public async Task SendMessage(List messages) 24 | { 25 | var completionResult = await _openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest 26 | { 27 | Messages = messages, 28 | Model = _model, 29 | }); 30 | 31 | if (completionResult.Successful) 32 | { 33 | return completionResult.Choices.First().Message.Content; 34 | }; 35 | 36 | throw new HttpRequestException(completionResult.Error.Message); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /PSCH/psch.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MaciejTrudnos/PowerShell-Command-History/b0549194f7297adc591978b40aa6fab080f1284b/PSCH/psch.db -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PowerShell Command History 2 | 3 | [![Build](https://github.com/MaciejTrudnos/PowerShell-Command-History/actions/workflows/dotnet.yml/badge.svg?branch=master)](https://github.com/MaciejTrudnos/PowerShell-Command-History/actions/workflows/dotnet.yml) 4 | 5 | Console application allow copy to clipboard selected item from list all of the commands that have been run from PowerShell 6 | 7 | ![PowerShell-Command-History](https://user-images.githubusercontent.com/35919087/163270057-0306d46f-588a-47ea-95ae-6fadefb3a424.gif) 8 | 9 | ## Features 10 | - Returns list of all the commands that have been run from PowerShell terminal 11 | - Filter the commands 12 | - Set page size list 13 | - Copy selected command to the clipboard 14 | - Save favourites commands 15 | - Integration with ChatGPT 16 | 17 | ## Installation 18 | Put the published version of the application into the program files directory e.g. 19 | ```sh 20 | C:\Program Files\PSCH 21 | ``` 22 | Add an environment variable by command in PowerShell as Administrator 23 | ```sh 24 | [Environment]::SetEnvironmentVariable("PATH", $Env:PATH + ";C:\Program Files\PSCH", [EnvironmentVariableTarget]::Machine) 25 | ``` 26 | 27 | To use ChatGPT, you need to add an OPEN_AI_API_KEY key to the environment variable. Your API key can be obtained from [here](https://platform.openai.com/account/api-keys) 28 | ```sh 29 | [System.Environment]::SetEnvironmentVariable("OPEN_AI_API_KEY", "", "Machine") 30 | ``` 31 | 32 | ## Command usage 33 | Print help 34 | ```sh 35 | psch -h 36 | ``` 37 | Print version 38 | ```sh 39 | psch -v 40 | ``` 41 | Set page size as 15 42 | ```sh 43 | psch -s 15 44 | ``` 45 | Save command 46 | ```sh 47 | psch -s 48 | ``` 49 | Remove command 50 | ```sh 51 | psch -r 52 | ``` 53 | Saved commands list 54 | ```sh 55 | psch -f 56 | ``` 57 | Start conversation with ChatGPT. Default model is GPT-4o. 58 | ```sh 59 | psch -gpt 60 | ``` 61 | 62 | Set gpt-3.5-turbo model and start conversation with ChatGPT. 63 | List of models can be obtained from [here](https://platform.openai.com/docs/models) 64 | ```sh 65 | psch -gpt gpt-3.5-turbo 66 | ``` 67 | 68 | ### Simple usage `psch -gpt` 69 | ![psch-gpt](https://github.com/MaciejTrudnos/PowerShell-Command-History/assets/35919087/20f4c112-bc14-427a-bed9-6ed371fc9f51) 70 | 71 | ## Tech 72 | - [Sharprompt](https://github.com/shibayan/Sharprompt) - Interactive command-line based application framework for C# 73 | - [.NET SDK for OpenAI](https://github.com/betalgo/openai) - A .NET SDK for accessing OpenAI's API, provided as a community library. 74 | 75 | ## License 76 | [Released under the MIT license.](https://github.com/MaciejTrudnos/PowerShell-Command-History/blob/master/LICENSE) 77 | --------------------------------------------------------------------------------