Capturing output of a windows command to a variable - cmd

I was looking for a Windows CMD equivalent to something like
var=`ls`
in Unix. Is there a way to do so without having to iterate using for /f?

You can do it with PowerShell (and script it in a .ps1).
PS C:\> $myVar = & dir | format-table -hidetableheaders -property name
PS C:\> echo $myVar
Program Files
Program Files (x86)
Users
Windows

Related

how to do taskkill for all programs in the folder using cmd / powershell

I want to do Taskkill for all programs ending with exe in a folder with cmd / powershell
Example
taskkill /f /im C:\folder\*.exe
In PowerShell, something like this:
$files = gci "C:\Path\To\Files" -Filter "*.exe"
foreach($file in $files){
Get-Process |
Where-Object {$_.Path -eq $file.FullName} |
Stop-Process -WhatIf
}
Remove the -WhatIf when you're confident that the correct processes would be stopped.
I would advise that you try a different built-in command utility, WMIC.exe:
%SystemRoot%\System32\wbem\WMIC.exe Process Where "ExecutablePath Like 'P:\\athTo\\Folder\\%'" Call Terminate 2>NUL
Just change P:\\athTo\\Folder as needed, remembering that each backward slash requires doubling. You may have difficulties with other characters in your 'folder' name, but those are outside of the scope of my answer. To learn more about those please read, LIKE Operator
Note: If you are running the command from a batch-file, as opposed to directly within cmd, then change the % character to %%
nimizen's helpful PowerShell answer is effective, but can be simplified:
Get-Process |
Where-Object { (Split-Path -Parent $_.Path) -eq 'C:\folder' } |
Stop-Process -WhatIf
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.
Note:
The above only targets processes whose executables are located directly in C:\folder, by checking whether the (immediate) parent path (enclosing directory) is the one of interest, using Split-Path -Parent.
If you wanted to target executables located in C:\Folder and any of its subfolders, recursively, use the following (inside Where-Object's script block ({ ... }):
$_.Path -like 'C:\folder\*'
Unlike Compo's helpful wmic.exe-based answer[1], this answer's solution also works on Unix-like platforms (using PowerShell (Core) 7+).
[1] Technically, wmic.exe is deprecated, as evidenced by wmic /? printing WMIC is deprecated in red, as the first (nonempty) line. Consider using PowerShell's CIM cmdlets, such as Get-CimInstance, instead, which has the added advantage of returning objects rather than text, for robust subsequent processing.

How to start git-bash with some argument on separate new window from Powershell

I am trying to do the following steps from Powershell. Following is just a pseudo-code which explains my requirements.
# This is a powershell function
function load(){
cd C:\my_path\scripts
invoke-expression -Command C:\Program Files\Git\git-bash.exe
# I want to go to this path into the git-bash.exe window
cd C:\my_path\scripts
# I have bash script here. I want to excuete this script.
./loadData.sh
}
how can I achieve this by Powershell? Thanks!
try git-bash.exe --help to see what parameters it has. after a short google search i assume you can run Start-Process -FilePath "C:\Program Files\Git\git-bash.exe" -ArgumentList '--cd="C:\my_path\scripts" --exec="loadData.sh"' maybe even Start-Process -FilePath "C:\Program Files\Git\git-bash.exe" -ArgumentList '--exec="C:\my_path\scripts\loadData.sh"' might work.
If you simply want to execute the bash script loadData.sh, run :
cd C:\my_path\scripts
path/to/bash.exe loadData.sh
This will create a bash shell (in the current console, not in a separate one), run your script, and exit, returning to your powershell.
If the current working directory is not set as you expect within your bash shell, you can perhaps pass it as an argument to your script :
# not 100% tested, I don't have a Powershell at hand
path/to/bash.exe loadData.sh /c/my_path/scripts
# and in your loadData.sh, add an instruction :
cd "$1"

Batch Rename at

I need help with a Command Prompt Script with regards to Bulk Renaming AVI & JPG files within their folders by using their respective "Folder Names", however the JPG file named "folder" must remain unchanged.
Example Before:
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984).avi
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984).jpg
C:\Temp\Videos\Terminator\Folder.jpg
Example After:
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984ST).avi
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984ST).jpg
C:\Temp\Videos\Terminator\Folder.jpg
Thanks in advance
Try with Windows PowerShell: see here how to open Windows PowerShell.
And try these commands line:
cd C:\Temp\Videos\"Terminator (1984ST)"
dir * | rename-item -NewName {$_.name -replace "1984","1984ST"}
Reference: PowerShell, commands

How can I write the pipe command Linux in Windows batch?

How can I write the | (Linux) command in a Windows cmd (batch file)?
I don't know how to write this little Linux script in Windows:
find -r * |grep *.fd | open
In Windows:
dir /S ??? open
I don't really know what open does. If it simply starts an associated application with the respective file, then the following should do it:
for /r %f in (*.fd) do (start "" "%f")
In PowerShell you can do the same with:
Get-ChildItem -Recurse -Filter *.fd | Invoke-Item
or shorter:
gci -rec -fi *.fd | ii
The regular command shell in windows is lacking in power and features. However, Windows Power Shell has the ability to run a lot of ninja commands similar to *nix shells.
You can get more information about power shell on MSDN - http://msdn.microsoft.com/en-us/library/aa973757%28v=vs.85%29.aspx
Here is an example I googled from Powershell help itself:
-------------------------- EXAMPLE 4 --------------------------
C:\PS>get-childitem
c:\windows\system32* -include *.txt
-recurse | select-string -pattern "Microsoft" -casesensitive
This command examines all files in the
subdirectories of C:\Windows\System32
with the .txt file extension, for the
string "Microsoft". The CaseSensitive
parameter indicates that the 'M' in
'Microsoft' must be capitalized and
the rest of the characters must be
lowercase for a match to occur.

How to use cmd type pipe (/piping) in PowerShell?

In cmd (and bash), pipe "|" pushes output to another command in the original format of the first command's output (as string).
In PowerShell, everything that comes out the pipe is an object (even a string is a string object).
Because of that, some commands fail when run in a PowerShell command window as opposed to a Windows command window.
Example:
dir c:\windows | gzip > test.gz
When this command is run in the Windows command prompt window it works properly - directory listing of C:\windows gets compressed into test.gz file.
The same command in PowerShell fails, because PowerShell does not use cmd-style pipe and replaces it with PowerShell pipe (working with array of file system items).
Q. How do you disable the default piping behavior in PowerShell to make traditional Windows commands work identically in PowerShell?
I tried using the escape character "`" before the pipe "`|", but it didn't work. I also tried invoke-expression -command "command with | here", but it also failed.
if you want to send strings down the pipeline you can use the cmdlet "out-string"
For Example:
get-process | out-string
If you are specifically looking for a PowerShell way to zip up files, check out the PowerShell Community Extensions. there are a bunch of cmdlets to zip and unzip all kinds of files.
http://pscx.codeplex.com
If you can pipe the output of (CMD) dir into gzip, then gzip apparently knows how to parse dir output. The (string) output from the PowerShell dir command (aka Get-ChildItem) doesn't look the same, so gzip likely would not be able to parse it. But, I'd also guess that gzip would be happy to take a list of paths, so this would probably work:
dir c:\windows | select -ExpandProperty FullName | gzip > test.gz
No warrantees express or implied.
If you really need to use the old school DOS pipe system in PowerShell, it can be done by running a command in a separate, temporary DOS session:
& cmd /c "dir c:\windows | gzip > test.gz"
The /c switch tells cmd to run the command then exit. Of course, this only works if all the commands are old school DOS - you can't mix-n-match them with PowerShell commands.
While there are PowerShell alternatives to the example given in the question, there are lots of DOS programs that use the old pipe system and will not work in PowerShell. svnadmin load is one that I've the pleasure of having to deal with.
You can't. PowerShell was designed to pass objects down a pipeline, not text. There isn't a backwards-compatability mode to DOS.

Resources