Powershell auto-completion using script file - windows

I wanted to execute a PowerShell script file script.ps1 like this:
powershell ./script.ps1
How will I simulate a 'tab' (auto-complete) feature in script file?
Eg : on the PowerShell prompt, I can do
PS> get [TAB]
for getting suggestions for commands starts with 'get-'
Same thing : how will I write in a script file, means saving the script.ps1 file like:
get-\t
and executing PowerShell ./script.ps1 resulting error instead of getting suggestion for command which starts with "get-"
Any idea to encode the tab event inside a script file to achieve the command auto completion ?

powershell get-command get-*
Is working

It's very unclear what you intend to do but here are the basics on tab-completion:
Save the PS1 file then you can type part of the file name and autocomplete will offer it among the suggestions (though this is done in alphabetical order for all possible suggestions that PowerShell knows.)
If you wish to get parameter tab completion you need to enabled the cmdlet (script) or function as an "advanced" function with a Param block which defines all of the Parameters you cmdlet or function accepts.
The typing: Get-MyCommand - # will result in the possible param and switch names being offered in turn.
Running PSReadline (available from the PowerShell Gallery an enhance the autocomplete feature for all commands and parameters.
If you are seeking to perform some other function you'll need to edit your question to be more specific and how clear examples....

Related

How to "pipe" console values into interactive Windows PowerShell command (to make it non-interactive)

Does Windows PowerShell have anything that's similar to piping user-supplied values into interactive Unix/Linux bash commands to make them run non-interactive?
What I mean is something like this:
https://www.igorkromin.net/index.php/2017/04/12/invoking-interactive-shell-scripts-non-interactively
I need to supply values directly via PowerShell instead of letting the user type them on the console.
What I mean is not something that would be equivalent to xargs in bash, as that would only allow me to supplant command line parameters. For the specific command that I have in mind, default values can be specified on the command line, but are not suitable for the specific task. There are not any other parameters that can be given on the command line - values other than default are normally given by user input.
The only thing I tried out was similar to how you would do it in bash:
echo VALUE | CMD
This didn't work as the command still asked for user input on the console.

How can I keep powershell in repl mode with a loaded script

I'm trying to start a powershell instance, that loads a script and remains open so I can still call methods loaded by that script manually.
I'm trying to dot source a script and pipe it to powershell like below, from a cmd instance/batchfile:
echo . .\script.ps1 | powershell
The result in this case is that powershell starts, loads my script, executes it and exits. I've tried running with -noexit argument, it has no effect.
I'm thinking of another option, to start a powershell process and pipe my dot source command to its stdin - but this probably won't allow me to interact with the process anymore because its stdin is opened by the host process.
If you need to run a script file so that window stays open and variables are accessible after the execution.
Try dot sourcing the script file like this:
powershell -noexit ". .\script.ps1"
Once the script is done, you can access any internal variable the script defined. Assuming the variables are at the script level scope.

calling another scripts to run in current script

I'm writing a shell script. what it does is it will create a file by the input that is received from the user. Now, i want to add the feature called "view a file" for my current script. Now, it's unreasonal to retype it again since i've already had a script that helps
I know it's crazy when it is possible to it with normal shell command. I'm actually writing a script that help me to create pages that are generated from the touch command. (this pages had attached date, author name, subjects, and title).
The question is how to call a another script or inhere another script?
Couple of ways to do this. My prefered way is by using source
You can -
Call your other script with the source command (alias is .) like this: source /path/to/script.
Make the other script executable, add the #!/bin/bash line at the top, and the path where the file is to the $PATH environment variable. Then you can call it as a normal command.
Use the bash command to execute it: /bin/bash /path/to/script

execute powershell commands with Lua

I have a program that I work with, that has an onboard lua compiler to allow for custom written actions.
Since the tool itself is very limited, especially if it goes for complex reactions over networks, I want to use Powershell over lua.
Methods like os.execute() or io.popen() use the standard command line from windows and not Powershell.
Is there a way to use Powershell with lua?
I tried to write a command line script with the Powershell editor and run this script with os.execute, but it opens it as a textfile, it would be better to write the commands directly in lua but if there is no other way, executing a Powershell script directly would also be fine. (In Windows itself you can execute the script with right mouse "click/Execute with Powershell")
-- You can generate PowerShell script at run-time
local script = [[
Write-Host "Hello, World!"
]]
-- Now create powershell process and feed your script to its stdin
local pipe = io.popen("powershell -command -", "w")
pipe:write(script)
pipe:close()
Your description of the problem makes it sound like you're using a command such as os.execute("powershellscript.ps1"), and that call invokes cmd.exe with your string as the proposed command line. Normally, Windows will open a .PS1 file for editing; this was a deliberate decision for safety. Instead, try altering the os.execute() command to explicitly call PS: os.execute("powershell.exe -file powershellscript.ps1"). If you need to pass parameters to your script, enclose them in {}. See https://msdn.microsoft.com/en-us/powershell/scripting/core-powershell/console/powershell.exe-command-line-help for more info on invoking PowerShell from the command line.

Automating Powershell script by using Powershell

I have a User Creation AD script which adds users automatically.
For the moment the script is ok, but i expect that the script has to be modified in the future.
For example i want to change this script:
<Departments>
<Department>Finance</Department>
<Department>IT</Department>
<Department>Marketing</Department>
<Department>Sales</Department>
<Department>Executive</Department>
<Department>Human Resources</Department>
<Department>Security</Department>
</Departments>
to
<Departments>
<Department>Finance</Department>
<Department>IT</Department>
<Department>Marketing</Department>
<Department>Sales</Department>
<Department>Executive</Department>
<Department>Human Resources</Department>
<Department>Security</Department>
<Department>Intern</Department>
</Departments>
So i want to add a line in the existing Powershell script, using another Powershell script.
This is just an example. In the future i want add a lot of code.
Is this possible ?

Resources