"wmic memorychip get | clip" in cmd - powershell

Hello i am not able to run this command:
wmic memorychip get | clip
This command does not returns any output or error.
I am using windows 7.
Kindly tell me what to do?
Thanks

This happens, as the output is piped to clip.exe. In other words, the output is there, but is sent to clipboard instead of printing it into the console.
As a side note, consider using Set-Clipboard instead of clip.exe, if you have Powershell 5.1 or newer.
If there's a need to send output to clipboard and see it too, use Tee-Object like so,
wmic memorychip get | tee-object -variable foo | set-clipboard; $foo

As vonPryz answered that the ouput is there but is redirected to clip.exe(A program that redirects output of command line tools to the Windows clipboard).but as you have described that you have windows 7,vonPryz solution won't work.you can use wmic memorychip to show output on console but it will mess up the output. you should echo the output in a file to see it(i.ewmic memorychip >>output.txt) or use the following batch file:
#echo off
wmic memorychip >>output.txt
notepad output.txt
pause >nul

Related

Redirect the output of a PowerShell script to a file using command prompt

How to redirect the output of a PowerShell 5.0 script to a file using cmd in Windows 10? I tried the following:
powershell ".\myscript.ps1 | Out-File outfile.txt"
and:
powershell .\myscript.ps1 > outfile.txt
to no avail. The outfile.txt is created but remains empty. The command prompt window is run with Administrator privileges.
In a script I use:
Write-Host $MyVar
Write-Host $SomeOtherVar
to output the values to a screen.
Use the specific PowerShell redirection operators:
(they appear to work at the (cmd) command prompt as wel)
Redirect the success stream (1>):
powershell .\myscript.ps1 1> outfile.txt
Redirect all streams (*>):
powershell .\myscript.ps1 *> outfile.txt
In a script I use:
Write-Host $MyVar
Write-Host $SomeOtherVar
to output the values to a screen.
Yeah, that's your problem right there! Write-Host writes the information straight to the screen buffer of the host application, so the standard output stream will never actually see anything.
Change your Write-Host statements to Write-Output (or just remove them):
Write-Output $MyVar
Write-Output $SomeOtherVar
# or simply
$MyVar
$SomeOtherVar

Right usage of 2>&1 | tee or | tee with powershell

I want to see the output of the command in the console and save it in a file so I have these two options:
When I use command | tee output.txt somehow it generates no output file at all but it works as usual in the console.
When I use command 2>&1 | tee output.txt it generates a fine output file but the text in the console appears in red.
Is there any way to either fix the first option or let the text appear as usual in the second one? I am using Windows PowerShell (Windows 10) and the Programm I am using this for is liquibase 3.5.5. just for the case that this is important.
In PowerShell, redirecting stderr lines from an external program to PowerShell's success stream via 2>&1 wraps those lines in [System.Management.Automation.ErrorRecord] instances in case they are captured for further processing.
In Windows PowerShell, these captured instances render like PowerShell errors, which is why you're seeing the red output (by contrast, without the redirection, the stderr lines would be passed through to the console, without coloring).
A simple workaround is to convert these objects to strings explicitly, which outputs the original lines (PSv3+ syntax; built-in alias % for ForEach-Object used for brevity):
... 2>&1 | % ToString | Tee-Object output.txt
Note: This workaround is no longer necessary in the install-on-demand, cross-platform PowerShell (Core) 7+ edition, where even captured stderr lines now consistently render just as strings.
If you are in DOS then you can leverage powershell (from DOS) and use tee-object do more or less a tee like in Linux.
C:\> powershell some_command ^| tee-object -FilePath some_outputfile
The ^ escapes the pipe so that the pipe applies to powershell and not DOS.
Powershell and tee-object should come with Windows as standard so nothing to install!

PowerShell Tee-Object command with -Append

I run into an issue with the PowerShell 4.0 Tee-Object command (alias tee) and the command that I'm using is as follows:
powershell "cmd /c dir | Tee-Object -filepath C:\1.txt -Append"
When 1.txt is an empty file, it writes the output as you see on the screen (as-is). But if 1.txt has some existing content before you run the command, it appends the output with no new lines and the files looks completely messed up.
I'm surprised to see this behavior and any thoughts would help me to proceed further. Note that I have to use the Tee-Object command as I would like to see the output on the screen when a command is running and append it to the existing log file that has some contents.

Capture snmpwalk output via powershell

I would like to run a program and save its output into a file. The program starts, I see a cmd window pop-up, but the file is alway empty. I tryed these versions:
start snmpwalk.exe "parameters will be here" >text.txt
start snmpwalk.exe "parameters will be here" 2>&1 | out-file text.txt
Can you help me how to solve the problem?
Thanks
You don't need to use start, you should be able to just use the command as normal from within powershell.
A slightly more powershell way would be to use invoke-expression
invoke-expression "snmpwalk [parameters]" | out-file text.text

Echo equivalent in PowerShell for script testing

I would like to output variables and values out in a PowerShell script by setting up flags and seeing the data matriculate throughout the script.
How would I do this?
For example, what would be the PowerShell equivalent to the following PHP code?
echo "filesizecounter: " . $filesizecounter
There are several ways:
Write-Host: Write directly to the console, not included in function/cmdlet output. Allows foreground and background colour to be set.
Write-Debug: Write directly to the console, if $DebugPreference set to Continue or Stop.
Write-Verbose: Write directly to the console, if $VerbosePreference set to Continue or Stop.
The latter is intended for extra optional information, Write-Debug for debugging (so would seem to fit in this case).
Additional: In PSH2 (at least) scripts using cmdlet binding will automatically get the -Verbose and -Debug switch parameters, locally enabling Write-Verbose and Write-Debug (i.e. overriding the preference variables) as compiled cmdlets and providers do.
Powershell has an alias mapping echo to Write-Output, so you can use:
echo "filesizecounter : $filesizecounter"
PowerShell interpolates, does it not?
In PHP
echo "filesizecounter: " . $filesizecounter
can also be written as:
echo "filesizecounter: $filesizecounter"
In PowerShell something like this should suit your needs:
Write-Host "filesizecounter: $filesizecounter"
Write-Host "filesizecounter : " $filesizecounter
By far the easiest way to echo in powershell, is just create the string object and let the pipeline output it:
$filesizecounter = 8096
"filesizecounter : $filesizecounter"
Of course, you do give up some flexibility when not using the Write-* methods.
echo is alias to Write-Output although it looks the same as Write-Host.
It isn't What is the difference between echo and Write-Host in PowerShell?.
echo is an alias for Write-Output, which writes to the Success output stream. This allows output to be processed through pipelines or redirected into files. Write-Host writes directly to the console, so the output can't be redirected/processed any further.
The Write-host work fine.
$Filesize = (Get-Item $filepath).length;
Write-Host "FileSize= $filesize";
It should also be mentioned, that Set-PSDebug is similar to the old-school echo on batch command:
Set-PSDebug -Trace 1
This command will result in showing every line of the executing script:
When the Trace parameter has a value of 1, each line of script is traced as it runs. When the parameter has a value of 2, variable assignments, function calls, and script calls are also traced. If the Step parameter is specified, you're prompted before each line of the script runs.
PowerShell has aliases for several common commands like echo. Type the following in PowerShell:
Get-Alias echo
to get a response:
CommandType Name Version Source
----------- ---- ------- ------
Alias echo -> Write-Output
Even Get-Alias has an alias gal -> Get-Alias. You could write gal echo to get the alias for echo.
gal echo
Other aliases are listed here:
https://learn.microsoft.com/en-us/powershell/scripting/learn/using-familiar-command-names?view=powershell-6
cat dir mount rm cd echo move rmdir chdir erase popd sleep clear h ps sort cls history pushd tee copy kill pwd type del lp r write diff ls ren
I don't know if it's wise to do so, but you can just write
"filesizecounter: " + $filesizecounter
And it should output:
filesizecounter: value
Try Get-Content .\yourScript.PS1 and you will see the content of your script.
also you can insert this line in your scrip code:
get-content .\scriptname.PS1
script code
script code
....