Autorunsc64 (sysinternals) command line issue in PowerShell - powershell

I am trying to execute autorunsc64.exe (Sysinternals) in PowerShell like so:
"C:\Program Files (x86)\Autoruns\autorunsc64.exe" -a * > "C:\Program Files (x86)\Autoruns\output.txt"
However, it does not like single or double quotes anywhere. I've tried many variations but cannot seem to find the solution. I keep getting the following errors:
Unexpected token '-accepteula' in expression or statement
Unexpected token '-a' in expression or statement
If the paths do not have spaces, it works without issue:
C:\Temp\Autoruns\autorunsc64.exe -accepteula -a * > C:\Temp\Autoruns\output.txt
How can I type this up to work with C:\Program Files (x86)\Autoruns\autorunsc64.exe -a * > C:\Program Files (x86)\Autoruns\output.txt so it can run from these locations using PowerShell?

Very common question.
& "C:\Program Files (x86)\Autoruns\autorunsc64.exe"
Or even
C":\Program Files (x86)\Autoruns\autorunsc64.exe"
I would just add it to the path.

Related

PowerShell command only works without spaces between arguments

I try certain source codes using PowerShell to extract an password protected archive using 7zip:
This command doesn' work (7zip is an alias for $7zipPath):
& 7zip x "$zipFile" -o "$output" -p $zipFilePassword
I get the this error:
Command Line Error:
Too short switch:
But when I remove the spaces between the variables -o and -p, the archive can be extracted. This behaviour confuses me with other command line tools like git etc.? Why is it so?
The behavior is specific to 7-Zip (7z.exe) and applies to whatever program (shell) you invoke it from:
Deviating from widely used conventions observed by CLIs such as git, 7z requires that even switches (options) that have mandatory arguments, such as -o and -p, have the argument directly attached to the switch name - no spaces are allowed:
& 7zip x $zipFile -o"$output" -p"$zipFilePassword"
Note that you normally need not enclose variable references in PowerShell in "..." (note how $zipFile isn't), even if they contain spaces. However, in order to attach them directly to switch names, you do.
Alternatively, you could enclose the entire token - switch name and argument - in double quotes:
& 7zip x $zipFile "-o$output" "-p$zipFilePassword"

how to eliminate command exception issue in gsutil

I have this command
gsutil rsync -r -x '".*.jpg$"' File Share\data\Home Drive gs://sdefs01/Home Drive
this is to exclude any .jpg file to be copied to my google bucket.
however, it returns an error:
commandexceptions: the rsync command accept at most 2 arguments.
the command example that I refer to is from google cloud support page.
please help.
You need to put the source directory path inside double quotes as it contains spaces.

running rsync in cygwin

I am trying to run rsync from cygwin in a PowerShell script of a remote folder with having space (File - One) and gets the below error. Tried to escape the space but still it does not work. As in the error, the directory name taken is "/e/Files/File" which is incomplete, whereas the actual folder name should be "/e/Files/File - One/root/" . Also tried the escape the space with multiple backslashes but still same error. What could be issue here?
Write-Output $rsync-cmd
Invoke-Expression -command $rsync_cmd
Command and Error:
C:\cygwin64\bin\bash.exe --login -c 'rsync -tvhPrI --stats jason#10.1.1.100:"/e/Files/File\ -\ One/root/" "/e/Files/File\ -\ One/root"'
receiving incremental file list
rsync: link_stat "/e/Files/File" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1637) [Receiver=3.1.1]
I've just been wrestling with this and all problems solved by running my script through dos2unix to remove rogue ^M

How to change the directory to program files using Powershell?

I would like to open a C:\Program Files\R\R-3.2.0\bin\Rscript.exe. For that I am trying to change the directory. I figured that the error is in opening Program files. Following is the code
cd Program Files\R\R-3.2.0\bin
Error: A positional parameter cannot be found that accepts argument Files
Unlike command.com/cmd.exe, PowerShell follows much more consistent rules and in the failing case Program and Files\R..bin are parsed as two separate arguments, where the second is invalid in context (as cd only accepts a single non-named argument).
To fix this use quotes, eg.
cd "C:\Program Files"
With the quotes it is parsed as a string value which is supplied as a single argument (the string itself does not include the quotes, again unlike cmd.exe rules).
FWIW, cd is an alias for Set-Location. Run get-help cd for the details on how it can be used - include which optional (and named) parameters it does support.
You need to put the path in quotes if it contains a space:
cd 'C:\Program Files\R\R-3.2.0\bin'
Either single or double quotes will work.

Equivalent for linux mkdir {fileA,fileB} in PowerShell

I'm just curios. Is there an equivalent for PowerShell that behaves equally to the liunx command listed in the title, i.e.
mkdir {folderA, folderB}
?
-- edit
the command listed above creates the folders "folderA" and "folderB" (just saw that I wrote file previously. Sorry, my fault) in the current working directory.
The mkdir command in PowerShell is a wrapper for the New-Item command. If you want to create multiple folders with a single command, then run:
mkdir c:\test,c:\test2;
Effectively, because of positional parameters in PowerShell, this passes the array c:\test,c:\test2 to the -Path parameter of the New-Item command.