Powershell Script Questions

134 views
Skip to first unread message

Kyle Condon

unread,
May 29, 2023, 5:01:38 PM5/29/23
to OneCommander
`Hey I'm trying to write scripts for OneCommander, but it's PS implementation is weird to me. How exactly is it running the scripts. It seems to be using Windows PowerShell v1.0 implementation somehow, but I was wondering if there's a way for it to use PowerShell v7+?

Or could you explain exactly how it calls PS when using #PS at the top of the script. It's functionality is different that a default Windows PowerShell v1.0 script. Particularly in the following areas:

-It treat's spaces in file paths that are in quotes as two separate commands.
-Commands and Parameters that exist in my system Windows Powershell v1.0 arsenal don't work. I tried replacing the System.Management.Automation.dll with Powershell 7's and that didn't work. I also tried decompiling the OneCommander.exe and that seems obfuscated.
-$CurrentDir returns in singles quotes and sometimes the end ending single quote will persist and has to be parsed, but a simple .Replace("'",""") doesn't work because of how OC handles double quotes. 
-What Shell does it use for scripts without a #Shell comment at the top of the file?

If you could provide Documentation or insight into this that would be great. I am planning to release my Scripts to your users, but it's hard writing Scripts due the workarounds I have to find. Thanks and have a nice day!

Milos Paripovic

unread,
Jun 2, 2023, 1:26:47 PM6/2/23
to OneCommander
So, OC is using Process Start to run it with shell

powershell.exe Invoke-Command -ScriptBlock {$SCRIPT}"

I will have to check if there is a way to do it with v7 of PS

Running the powershell this way is messy with the way the quotes but it was working in tests I ran. 
Each file and folder passed with $Sel or $CurrentDir
is already wrapped in ' for that to work
but you can also use $SelNoQuotes and $CurrentDirNoQuotes that doesn't wrap them

There is one workaround I had to add to support names with ' in them, and weirdly, that has to become '' (two times '  ) for the script to work

If you use $Sel it will run the script individually for each file
If you use $SelMultiple it will add all fullpaths space separated at the place if that argument

If there is no #PS on top it will run it as
cmd.exe /k $SCRIPT

It was implemented that way so OC can get the output and errors the scripts return.

This part is still a beta stage, as I didn't receive much feedback on what users actually expect with this. My tests and needs were just running mostly on-liner simple scripts so I am not sure how it works in more complicated workflows or where things don't work as expected, so any feedback for improvement is welcome.

Kyle Condon

unread,
Jun 3, 2023, 4:01:45 AM6/3/23
to OneCommander
$SelNoQuotes, $CurrentDirNoQuotes, and $OpDirNoQuotes don't exist for me.

$SelMultiple is unmanageable because it returns a text swap of single quote wrapped items. I don't know of a way to tell powershell.exe to take the text swap in it's entirety as a string while limited to single quotes. I've tried @' $SelMultiple '@ but it doesn't pass as a string to another script via parameter. 


Milos Paripovic

unread,
Jun 3, 2023, 12:32:46 PM6/3/23
to OneCommander
I see that I swapped the order so it doesn't work the NoQuotes for Powershell, so I'll fix it in the next update.
But can you clarify the second part? How would you need to script output to look like when using SelMultiple?

Do you need csv-s or similar?

I asked ChatGPT since I didn't se PS much

# Define the paths as an array $paths = "C:\path1", "C:\path2", "C:\path3" # Convert the array to a single string separated by a delimiter (e.g., semicolon) $pathsString = $paths -join ";" # Call ScriptB.ps1 and pass the paths as a string parameter & "Path\To\ScriptB.ps1" -Paths $pathsString


And then split it again in another script 

param ( [Parameter(Mandatory=$true)] [string]$Paths ) # Convert the paths string to an array using the delimiter (e.g., semicolon) $SelArray = $Paths -split ";"

Would it be more useful if program would already output the selection as a variable that PS can iterate on instead of using Search&Replace from the program?
; doesn't seem like a good solution as it is a filename legal character, so maybe NewLine, as $SelAsMultiLineString
or to already create an array by doing
$SelArray   = $multiLineString.Split([Environment]::NewLine)

and then you have array as param that you can iterate over?
What would be most convenient from script making perspective?

Some sample use case would also be useful  

and
and

Kyle Condon

unread,
Jun 3, 2023, 3:42:50 PM6/3/23
to OneCommander
Okay I figured it out, you have to be able to do:
$var = @'
@SelMultiple
'@

Script.ps1 -SelMultiple $var

Then parse it in the script file. 

Kyle Condon

unread,
Jun 3, 2023, 3:43:25 PM6/3/23
to OneCommander
$SelMultiple not @SelMultiple

Milos Paripovic

unread,
Jun 3, 2023, 5:37:29 PM6/3/23
to OneCommander
So it would output them in separate lines without any " or ' ?

c:\folder with spaces\x.txt
d:\File with apostrophe'.txt


Kyle Condon

unread,
Jun 4, 2023, 1:19:04 PM6/4/23
to OneCommander
No I had to do a literal string or it would list all selected file paths surrounded in single quotes with spaces but no wrapping quotes on the whole string so it would interpret them as commands.

Milos Paripovic

unread,
Jun 4, 2023, 8:35:25 PM6/4/23
to OneCommander
Can you try this version just running from a different folder (close current one)

It is creating $SelectedFiles variable that you can use from PowerShell. 
Exposing variables seems better approach than doing Search&Replace.

In the background it is doing 
$SelectedFiles  = @'
C:\File1
D:\File2
...
'@
and this works even when filenames have ' in them

Kyle Condon

unread,
Jun 8, 2023, 11:11:20 AM6/8/23
to OneCommander
Okay when I pass $SelectedFiles to my script and ```Write-Warning $SelectedFiles``` it outputs how you described but 4 times over. It display 4 warnings with the variables. Like it's calling the script(.ps1) multiple times even though it's called once in the script(OC).

Kyle Condon

unread,
Jun 8, 2023, 11:13:49 AM6/8/23
to OneCommander
Actually it repeats it for as many files as you have selected.

Kyle Condon

unread,
Jun 8, 2023, 11:15:44 AM6/8/23
to OneCommander
And Also tried not calling a .ps1 script and just writing it to warning output in the OC script and that repeats as well for as many files as are selected.

Kyle Condon

unread,
Jun 10, 2023, 3:15:42 AM6/10/23
to OneCommander
Found a work around by doing:
#PS
$SelectedFiles | Export-Clixml
$SelFilesParsed = Import-Clixml

This avoids all the previous problems. I just feed $SelFilesParsed to my script as an array parameter and all is well.

Kyle Condon

unread,
Jun 10, 2023, 4:34:49 PM6/10/23
to OneCommander
This is what I'm working on btw:

You run Install.ps1 -OCResourcesScriptsPath "PathToOCScriptsFolder"
Then you can use the scripts for OC. It will check for a Windows Terminal installation and install it via winget if it's missing. Same with PowerShell 7. Then it copies the OC scripts and PS scripts to the correct locations and Runs the basic init for the variables.

Add Directory Variable takes the current directory and stores it in a clixml file while adding code to $PROFILE to create variables on PS start.
All the scripts in the Terminal SubDir should work provided you have wsl and those particular distros installed. You can even execute a .ps1 file with a WT PS interactive shell and get output as well. Try it out.

Milos Paripovic

unread,
Jun 10, 2023, 10:35:18 PM6/10/23
to OneCommander
Thanks for sending this! I have looked through the repository and it is quite elegant. One small improvement could be checking already for install if OC is installed in %ProgramFiles%\OneCommander as an easier way to find Scripts folder as that is the default location for MSI installer. I will look more into it in the next days. 

I have found my mistake with repeating powershell commands if multiple files are selected and fixed it in 3.45.1 that is available now on main site
This version will run it once if the script uses $SelectedFiles but will still run it repeatedly if there is just $Sel 
($Sel is still search&replace for previous version consistency, while $SelectedFiles is an actual powershell variable)

Kyle Condon

unread,
Jun 11, 2023, 10:58:29 AM6/11/23
to OneCommander
Awesome, I am testing with the new OC version now. Also $Sel isn't really necessary as $SelectedItems[0] will return a single selected file.

Kyle Condon

unread,
Jun 11, 2023, 10:25:23 PM6/11/23
to OneCommander
Is there anyway you could add a tag like #PS but #PSINTERACTIVE so that variables can be input via the PS window without calling a whole other script to prompt the user and save it to disk? Or possibly also a way to add Documentation for the Scripts?

Milos Paripovic

unread,
Jun 11, 2023, 11:49:52 PM6/11/23
to OneCommander
Any examples, and how ideally the interaction would look like from user's point of view?

Kyle Condon

unread,
Jun 12, 2023, 12:08:01 AM6/12/23
to OneCommander
I thought about it and it's probably just better to spawn a winforms dialog with a textbox for input anyways. Prompts confuse people.
Reply all
Reply to author
Forward
0 new messages