{"id":668,"date":"2025-08-29T10:22:45","date_gmt":"2025-08-29T10:22:45","guid":{"rendered":"https:\/\/davidka.net\/?p=668"},"modified":"2025-09-21T04:27:32","modified_gmt":"2025-09-21T02:27:32","slug":"lets-embed-ai-in-powershell-part-1","status":"publish","type":"post","link":"https:\/\/ru.davidka.net\/ru\/lets-embed-ai-in-powershell-part-1\/","title":{"rendered":""},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"392\" src=\"https:\/\/davidka.net\/wp-content\/uploads\/2025\/08\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2025-09-21-052555-1024x392.png\" alt=\"\" class=\"wp-image-2978\" srcset=\"https:\/\/ru.davidka.net\/wp-content\/uploads\/2025\/08\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2025-09-21-052555-1024x392.png 1024w, https:\/\/ru.davidka.net\/wp-content\/uploads\/2025\/08\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2025-09-21-052555-300x115.png 300w, https:\/\/ru.davidka.net\/wp-content\/uploads\/2025\/08\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2025-09-21-052555-768x294.png 768w, https:\/\/ru.davidka.net\/wp-content\/uploads\/2025\/08\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2025-09-21-052555.png 1284w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In this experiment, I use <code class=\"\" data-line=\"\">Gemini-CLI<\/code> as a search model.<\/p>\n\n\n\n<div id=\"rtoc-mokuji-wrapper\" class=\"rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default\" data-id=\"668\" data-theme=\"Neve - Davidka\">\n\t\t\t<div id=\"rtoc-mokuji-title\" class=\"rtoc_btn_none rtoc_left\">\n\t\t\t\n\t\t\t<span>In Questo Articolo<\/span>\n\t\t\t<\/div><ul class=\"rtoc-mokuji mokuji_none level-3\"><li class=\"rtoc-item\"><a href=\"#rtoc-1\">What is <code class=\"\" data-line=\"\">Gemini CLI<\/code>?<\/a><ul class=\"rtoc-mokuji mokuji_none level-3\"><li class=\"rtoc-item\"><a href=\"#rtoc-2\">**Installation and Setup**<\/a><ul class=\"rtoc-mokuji mokuji_none level-3\"><li class=\"rtoc-item\"><a href=\"#rtoc-3\">The Terrible <code class=\"\" data-line=\"\">Invoke-Expression<\/code><\/a><\/li><li class=\"rtoc-item\"><a href=\"#rtoc-4\">Putting It All Together: The <code class=\"\" data-line=\"\">Invoke-Gemini<\/code> Cmdlet<\/a><\/li><li class=\"rtoc-item\"><a href=\"#rtoc-5\">Trying the Magic!<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ol><\/div><h4 id=\"rtoc-1\"  class=\"wp-block-heading\">What is <code class=\"\" data-line=\"\">Gemini CLI<\/code>?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">I have already talked in detail about **<code class=\"\" data-line=\"\">Gemini CLI<\/code>** in <a href=\"https:\/\/pikabu.ru\/series\/geminicli_48168\"><code class=\"\" data-line=\"\">Gemini CLI<\/code>: Introduction and First Steps<\/a>. But if you missed it, here&#8217;s a brief introduction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In short, **<code class=\"\" data-line=\"\">Gemini CLI<\/code>** \u2014 is a command-line interface for interacting with <code class=\"\" data-line=\"\">Google<\/code>&#8216;s <code class=\"\" data-line=\"\">AI<\/code> models. You run it in your terminal, and it turns into a chat that, unlike web versions, has access to your file system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key features:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Understands code:<\/strong> It can analyze your scripts, find errors in them, and suggest fixes.<\/li>\n\n\n\n<li><strong>Generates code:<\/strong> You can ask it to write a <code class=\"\" data-line=\"\">PowerShell<\/code> script to solve your problem, and it will.<\/li>\n\n\n\n<li><strong>Works with files:<\/strong> Can read files, create new ones, and make changes to existing ones.<\/li>\n\n\n\n<li><strong>Runs commands:<\/strong> Can execute shell commands, such as <code class=\"\" data-line=\"\">git<\/code> or <code class=\"\" data-line=\"\">npm<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For our purposes, the most important thing is that <code class=\"\" data-line=\"\">Gemini CLI<\/code> can work in **non-interactive mode**. That is, we can pass it a prompt as a command-line argument, and it will simply return an answer, without launching its interactive chat. This is the capability we will use.<\/p>\n\n\n\n<h4 id=\"rtoc-2\"  class=\"wp-block-heading\">**Installation and Setup**<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To get started, we need to prepare our environment. This is done once.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: Install <code class=\"\" data-line=\"\">Node.js<\/code><\/strong><br><code class=\"\" data-line=\"\">Gemini CLI<\/code> \u2014 is an application written in <code class=\"\" data-line=\"\">Node.js<\/code> (a popular <code class=\"\" data-line=\"\">JavaScript<\/code> runtime environment). So first, we need to install <code class=\"\" data-line=\"\">Node.js<\/code> itself.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to the official website: <a href=\"https:\/\/nodejs.org\/\">https:\/\/nodejs.org\/<\/a><\/li>\n\n\n\n<li>Download and install the **<code class=\"\" data-line=\"\">LTS<\/code>** version. This is the most stable and recommended option. Just follow the installer instructions.<\/li>\n\n\n\n<li>After installation, open a new <code class=\"\" data-line=\"\">PowerShell<\/code> window and check that everything is working: <code class=\"\" data-line=\"\">node -v npm -v<\/code> You should see versions, for example, <code class=\"\" data-line=\"\">v20.12.2<\/code> and <code class=\"\" data-line=\"\">10.5.0<\/code>.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2: Install <code class=\"\" data-line=\"\">Gemini CLI<\/code> itself<\/strong><br>Now that we have <code class=\"\" data-line=\"\">npm<\/code> (the package manager for <code class=\"\" data-line=\"\">Node.js<\/code>), installing <code class=\"\" data-line=\"\">Gemini CLI<\/code> comes down to one command. Run it in <code class=\"\" data-line=\"\">PowerShell<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code class=\" language-python\" data-line=\"\">npm install -g @google\/gemini-cli\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code class=\"\" data-line=\"\">-g<\/code> flag means &#171;global installation,&#187; which will make the <code class=\"\" data-line=\"\">gemini<\/code> command available from anywhere in your system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3: Authentication<\/strong><br>The first time you launch <code class=\"\" data-line=\"\">Gemini CLI<\/code>, it will ask you to sign in to your <code class=\"\" data-line=\"\">Google<\/code> account. This is necessary so that it can use your free quota.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Simply type the command in <code class=\"\" data-line=\"\">PowerShell<\/code>: <code class=\"\" data-line=\"\">gemini<\/code><\/li>\n\n\n\n<li>It will ask you about signing in. Select &#171;Sign in with <code class=\"\" data-line=\"\">Google<\/code>.&#187;<\/li>\n\n\n\n<li>In your browser will open a standard <code class=\"\" data-line=\"\">Google<\/code> sign-in window. Sign in to your account and grant the necessary permissions.<\/li>\n\n\n\n<li>After that, you will see a welcome message from <code class=\"\" data-line=\"\">Gemini<\/code> in the console. Congratulations, you are ready to work! You can type <code class=\"\" data-line=\"\">\/quit<\/code> to exit its chat.<\/li>\n<\/ol>\n\n\n\n<h4 id=\"rtoc-3\"  class=\"wp-block-heading\">The Terrible <code class=\"\" data-line=\"\">Invoke-Expression<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Before we put it all together, let&#8217;s get acquainted with one of the most dangerous cmdlets in <code class=\"\" data-line=\"\">PowerShell<\/code> \u2014 <code class=\"\" data-line=\"\">Invoke-Expression<\/code>, or its short alias <code class=\"\" data-line=\"\">iex<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code class=\"\" data-line=\"\">Invoke-Expression<\/code> takes a text string and executes it as if it were a command typed in the console.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code class=\" language-python\" data-line=\"\">$commandString = &quot;Get-Process -Name &#039;chrome&#039;&quot;\nInvoke-Expression -InputObject $commandString\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command will do the same as a simple call to <code class=\"\" data-line=\"\">Get-Process -Name &#039;chrome&#039;<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why is it dangerous?<\/strong> Because executing a string that you don&#8217;t control (for example, obtained from the internet or from <code class=\"\" data-line=\"\">AI<\/code>), \u2014 is a huge security hole. If the <code class=\"\" data-line=\"\">AI<\/code>, by mistake or with malicious intent, returns the command <code class=\"\" data-line=\"\">Remove-Item -Path C:\\ -Recurse -Force<\/code>, <code class=\"\" data-line=\"\">iex<\/code> will execute it without hesitation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For our task \u2014 creating a managed and controlled bridge between a natural language request and its execution \u2014 it is perfectly suited. We will use it with caution, fully aware of the risks.<\/p>\n\n\n\n<h4 id=\"rtoc-4\"  class=\"wp-block-heading\">Putting It All Together: The <code class=\"\" data-line=\"\">Invoke-Gemini<\/code> Cmdlet<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s write a simple <code class=\"\" data-line=\"\">PowerShell<\/code> function that will allow us to send prompts with a single command.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Copy this code and paste it into your <code class=\"\" data-line=\"\">PowerShell<\/code> window to make it available in the current session.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code class=\" language-python\" data-line=\"\">function Invoke-Gemini {\n    &lt;#\n    .SYNOPSIS\n        Sends a text prompt to Gemini CLI and returns its response.\n    #&gt;\n    &#091;CmdletBinding()]\n    param(\n        &#091;Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]\n        &#091;string]$Prompt\n    )\n    process {\n        try {\n            # Check if the gemini command is available\n            $geminiCommand = Get-Command gemini -ErrorAction Stop\n        }\n        catch {\n            Write-Error &quot;Command &#039;gemini&#039; not found. Make sure Gemini CLI is installed.&quot;\n            return\n        }\n        Write-Verbose &quot;Sending prompt to Gemini CLI...&quot;\n        # Run gemini in non-interactive mode with our prompt\n        $output = &amp; $geminiCommand.Source -p $Prompt 2&gt;&amp;1\n        if (-not $?) {\n            Write-Warning &quot;Gemini command finished with an error.&quot;\n            $output | ForEach-Object { Write-Warning $_.ToString() }\n            return\n        }\n        # Return clean output\n        return $output\n    }\n}\n<\/code><\/pre>\n\n\n\n<h4 id=\"rtoc-5\"  class=\"wp-block-heading\">Trying the Magic!<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s ask it a general question directly from our <code class=\"\" data-line=\"\">PowerShell<\/code> console.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code class=\" language-python\" data-line=\"\">Invoke-Gemini -Prompt &quot;Tell me about the five latest trends in machine learning&quot;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Congratulations!<\/strong> You have just successfully embedded <code class=\"\" data-line=\"\">AI<\/code> in <code class=\"\" data-line=\"\">PowerShell<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the next article, I will tell you how to use <code class=\"\" data-line=\"\">Gemini CLI<\/code> to run scripts and automate tasks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this experiment, I use Gemini-CLI as a search model. In Questo Articolo What is Gemini CLI? **Installation and Setup** The Terrible Invoke-Expression Putting It All Together: The Invoke-Gemini Cmdlet Trying the Magic! What is Gemini CLI? I have already talked in detail about **Gemini CLI** in Gemini CLI: Introduction and First Steps. But if&hellip;&nbsp;<\/p>\n","protected":false},"author":1,"featured_media":2978,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"left","neve_meta_author_avatar":"on","neve_post_elements_order":"[\"content\",\"tags\",\"comments\"]","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","_themeisle_gutenberg_block_has_review":false,"footnotes":""},"categories":[318],"tags":[],"class_list":["post-668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lets-embed-ai-in-powershell"],"acf":[],"_links":{"self":[{"href":"https:\/\/ru.davidka.net\/ru\/wp-json\/wp\/v2\/posts\/668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ru.davidka.net\/ru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ru.davidka.net\/ru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ru.davidka.net\/ru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ru.davidka.net\/ru\/wp-json\/wp\/v2\/comments?post=668"}],"version-history":[{"count":0,"href":"https:\/\/ru.davidka.net\/ru\/wp-json\/wp\/v2\/posts\/668\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ru.davidka.net\/ru\/wp-json\/wp\/v2\/media\/2978"}],"wp:attachment":[{"href":"https:\/\/ru.davidka.net\/ru\/wp-json\/wp\/v2\/media?parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ru.davidka.net\/ru\/wp-json\/wp\/v2\/categories?post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ru.davidka.net\/ru\/wp-json\/wp\/v2\/tags?post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}