Powershell problem with defining the path for a file pls help - Page 1
Forum section: electronics / general

author: Chriss
author_rank: Frequent Contributor
timestamp: March 17, 2021, 04:28:08 pm
content: Hi!I'm stumbled into a small problem with my script and need help about to figure out how so solve my problem.I have to start a 1.ps1 file from a .bat file which are on my usb stick.Everything is working like a charm until I change my PC and put the usb drive to the second pc.The problem is the path, the drive letter of the usb stick which can be different on different pc's.Here is my code where the path is defined as "F:\X\1.ps1":Code:PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""F:\X\1.ps1""' -Verb RunAs}"How can I run the 1.ps1 file or how to declare the path in a way does it work automatically when not an F drive isassigned to my usb stick?There is no way to predict on what pc what drive letter will be assigned.But the 1.ps1 file will always be in the X folder on the usb stick.Thanks for any sugestion or help.

author: AntiProtonBoy
author_rank: Frequent Contributor
timestamp: March 17, 2021, 10:35:15 pm
content: QuoteWhat you can do is manually assign the drive letter to your usb stick, like Z:\The next time you plug it in, Windows might recognise the device and reassign Z:\A more robust solution is enumerate all the available drives and then select the one with a particular volume label, or if it has the path you want to access. SeeGet-PSDrive.

author: Chriss
author_rank: Frequent Contributor
timestamp: March 17, 2021, 10:54:05 pm
content: Thank you m8 for your response.I come also to a similar solution like you mentioned, but I was not sure and maybe there would beanother solution what I cant figure out.But now, as you also say this will be hard to solve in a dynamical way.I hate this in Powershell.Such a framework and it cant handle such a simple task, what is simple to do in the old cmd...

author: gmb42
author_rank: Frequent Contributor
timestamp: March 18, 2021, 11:39:20 am
content: How is this a PowerShell issue? From what I can see it's the .bat file that's supplying the incorrect absolute path to the invocation of the PowerShellScript.I'm assuming that the .bat and .ps1 file are on the same USB device. You could either use a relative path for the script, e.g.Code:PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "".\X\1.ps1""' -Verb RunAs}"or you can use batch statements to extract the drive letter of the batch script itself and concat that to the PowerShell script path, e.g.Code:set ScriptPath=%~d0\X\1.ps1PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%ScriptPath%""' -Verb RunAs}"The batch path extractors are listedhere.

author: Chriss
author_rank: Frequent Contributor
timestamp: March 18, 2021, 10:54:32 pm
content: Thank you very much for this help.It works like charm.Can you pls help me to figure out how to do this:How can I in pwsh combine two variables in a wayto use that result as a file name?Something like this:Var1 = input from the userWrite the var1 + ".txt" to disk.I cant figure out how to combain thepart "var1+.txt" in pwsh so i get thecomplete filename.Lets say in my batch file I read the filenamein the set /p name="Input file name:"sysinfo > %name%.txtHow can I achieve this in pwsh?Or how to put together two sting in one variable?Thank you.

author: gmb42
author_rank: Frequent Contributor
timestamp: March 19, 2021, 10:58:46 am
content: PowerShell variables are prefixed by "$". You can use the "+" operator to join strings although there are many other ways to do that.Code:> $a = "Hello"> $b = "World"> $a + $bHelloWorld> $a + " " + $bHello World> "$a $b"Hello World> "My output: {0} {1}" -f $a, $bMy output: Hello World