0

I have a script to gerate information inside a folder.

Very basic example:

PS C:\Users\> write-host "teste"
teste
PS C:\Users\>

I want open powershell in silent mode, paste the write-host "teste" command and only show the result:

teste

Also, I don't want the PS C:\Users\>

I know I can create a ps1 script and run, but I need to navigate manually through PowerShell.

Resume: I don't want to see what I type in PowerShell command line, only the result.

If possible, turn on and turn off after execution, something like it:

PS C:\Users\> <enable silent mode>
    (I paste the commands here, but is not showed in the powershell command line)
    teste
             <disable silent mode>
PS C:\Users\> now I can work again....

About the first comment, this is the reason. How I see after paste the commands:

PS C:\Users> set-location C:\Users\xxxxxxxxxxxxxx
PS C:\Users\> $ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
PS C:\Users\> $CUR_DIR = "spool ""$pwd\"
PS C:\Users\> $DIR = ($CUR_DIR| Select-String -Pattern  "(?<!;)XX.*").Matches.Value.replace('\','')
PS C:\Users\> write-host $CUR_DIR$DIR".log"""
spool "C:\Users\xxxx.log"
PS C:\Users\> set-location xxxxxxxxxxxx
PS C:\Users> foreach ($row in $(Get-ChildItem| % { $_.FullName }| Sort-Object $ToNatural))
>> {
>> write-host "@""$row"""
>> write-host "show erros"
>> }
@"C:\Users\xxxxxxxxxx.txt"
show erros
@"C:\Users\xxxxxxxxxx.txt"
show erros
@"C:\Users\xxxxxxxxxx.sql"
show erros
PS C:\Users\xxxxxxxxxxxxxx> cd ..
PS C:\Users\> write-host "spool off"
spool off
PS C:\Users\>
PS C:\Users\> foreach ($row in $(Get-ChildItem| % { $_.FullName }| Sort-Object $ToNatural)|Select-String 'registro')
>> {
>> write-host "@""$row"""
>> }
@"C:\Users\xxxxxxxxxxxxxx\xxxxx.txt"
PS C:\Users\>

I'd like to see:

PS C:\Users> set-location C:\Users\xxxxxxxxxxxxxx
PS C:\Users\> <enable silent mode>
spool "C:\Users\xxxx.log"
@"C:\Users\xxxxxxxxxx.txt"
show erros
@"C:\Users\xxxxxxxxxx.txt"
show erros
@"C:\Users\xxxxxxxxxx.sql"
show erros
spool off
@"C:\Users\xxxxxxxxxxxxxx\xxxxx.txt"
           <disable silent mode>
PS C:\Users\> now I can work again....

I have replaced some information with xxx to don't show customer content.

5
  • 2
    This sounds quite useful for installing a virus without the user realizing. But are there any legal uses?
    – Aganju
    Commented Jun 10, 2022 at 0:42
  • Agree, it's useful for installing a virus. But I have edited the question with the reason.
    – Astora
    Commented Jun 10, 2022 at 0:58
  • @Aganju, I have a thought. Maybe there is a way to execute all those commands as a single command. This would help as well since the all results will be return after the command execution. Not necessary need to be in silent mode, I just need to see the results clearly to be able to copy. If someone knows how to do it, will be helpful.
    – Astora
    Commented Jun 10, 2022 at 1:13
  • There are several ways. The best way is to pipe to null like you would in other language.. only backward.. for instance, in cmd.exe you would do something like command_here >NULL where in powershell, you would do something like $Null = command_here will show no output and still run the command. Is that what you are asking? @Aganju ... I pipe STDOUT and STDERR commands to null all of the time in every language I code in and I don't write viruses. Commented Jun 10, 2022 at 1:38
  • If you need to run multiple commands and want them all piped to NULL, use this same method but wrap it in a script. Commented Jun 10, 2022 at 1:44

1 Answer 1

1

Use a script to hold all the commands, and they will not be written to the console:

# script.ps1 contents:
set-location C:\Users\xxxxxxxxxxxxxx
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
$CUR_DIR = "spool ""$pwd\"
$DIR = ($CUR_DIR| Select-String -Pattern  "(?<!;)XX.*").Matches.Value.replace('\','')
write-host $CUR_DIR$DIR".log"""

set-location xxxxxxxxxxxx
foreach ($row in $(Get-ChildItem| % { $_.FullName }| Sort-Object $ToNatural)) {
  write-host "@""$row"""
  write-host "show erros"
}

cd ..
write-host "spool off"

foreach ($row in $(Get-ChildItem| % { $_.FullName }| Sort-Object $ToNatural)|Select-String 'registro') {
  write-host "@""$row"""
}

Then just run the script from the console as needed:

PS C:\Users\> c:\path\to\script.ps1

# output looks like your "want to see" block

If your script execution policy doesn't allow for this, or you don't want to save a file, you can instead write all the commands to your terminal using shift+enter to run them all at once. This keeps the output together for easy copy/paste:

PS C:\Users> Write-Host "hello world"
>> Write-host Foo
>> Write-Host Bar

hello world
Foo
Bar

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .