How do I set these paths in powershell? - powershell

Good Day
I am trying to set these paths in powershell but have not been successful in this regard. I have a DOS script that was initially written to set these paths but now I need it set in powershell.
Primarily, these paths are still used in the script to parse information, so in the same breathe, should act as a variable as well. Please help if possible. I am aware that powershell has a Set-Location function but would it serve the purpose as described above. Here are the paths scripted in DOS below:
set CLASSPATH=D:\NetExpress\Base\Bin\WIN64\mfcobol.jar;.;%CLASSPATH%
set COBIDY=D:\NetExpress\Base\SOURCE
set COBLINK64=D:\NetExpress\Base\Bin\Linker
set COBREG_64_PARSED=True
set INCLUDE=D:\NetExpress\Base\INCLUDE
set LIB=D:\NetExpress\Base\Lib\WIN64
set MFTRACE_LOGS=D:\NetExpress\Base\MFTRACE\Logs
set SCHEMA_PATH=D:\NetExpress\Base\SCHEMA
set TXBIN=D:\NetExpress\Base
set TXDIR=D:\NetExpress\Base
set PATH=D:\NetExpress\Base\Bin\Linker;D:\NetExpress\Base\Bin\WIN64;D:\NetExpress\Base\Bin;D:\NetExpress\PACKAGES\x64;C:\Windows\System32\wbem;C:\Windows\System32\
set COBPATH64=D:\NetExpress\Base\Bin\WIN64
set LD_LIBRARY_PATH=D:\NetExpress\Base\Bin\WIN64;%int%
set COBDIR=D:\NetExpress\Base\Bin\WIN64;%int%;D:\NetExpress\MFSQL\SOURCE
set COBDIR64=D:\NetExpress\Base\Bin\WIN64
set COBPATH=%exe%
set COBCPY=%int%\LIBRYATM;%int%\LIBRYBOR;%int%\LIBRYBRO;%int%\LIBRYCHG;%int%\LIBRYCTA;%int%\LIBRYDCS;%int%\LIBRYGEN;%int%\LIBRYEN;%int%\LIBRYINV;%int%\LIBRYMIS;%int%\LIBRYSPY;%int%\LIBRYSWI;%int%\LIBRYTRE;%int%\LIBRYUNX;%int%\LIBRYCNV;D:\NetExpress\Base\SOURCE
set LogFile=%CD%\logfile.log
Assist if possible please...

It's as Mathias R. Jessen has mentioned in the comments
You'll have to use the in-built powershell variable $env.
$env has all the environment variables already defined in your system and you can add to them using
$env::VariableName = Value
Example would be something like
$env:CLASSPATH = "D:\NetExpress\Base\Bin\WIN64\mfcobol.jar;.;" + $env:CLASSPATH
If setting environment variable is your only goal then this should do it, if you are planning to use these scripts later in the same script file I'd suggest you use powershell variables instead.
Also check out this Microsoft Page for more on Environment Variables and Powershell

Related

Is there a limit to the amount of directories I can add to PATH

I've run into an issue recently where adding a new PATH variable deletes the previous one added. However, I need to add both of these variables to PATH in order to run a python wrapper I'm working on from command line. Does this mean there is a limit to the number of variables I can add to path? And can I delete some path variables I no longer use?
For reference, I'm running Windows 10 and have been adding variables to path by going through from Control Panel to Advanced System Settings and then going from there to Environment Variables. There you can add to your PATH variable by clicking new.
Thanks in advance.
Just figured out that one way to circumvent this is to add multiple directories to PATH at once, separating them with semicolons. For example,
Variable Name = PATH
Variable Value = C:\path\to\directory1;C:\path\to\directory2\;C:\path\to\directory3

How to set variables used in tasks?

I would like to set a variable to contain e.g. the path to a toolchain, that could be used in serveral tasks on a per project basis.
Does VSCode support this?
In my search for a solution the only answer i could find is to use environment variables. But i would like to be able to set this separately for each project/workspace e.g. in the c_cpp_properties.json file.

Execute PowerShell function on change of path

Is it possible to execute some code (eg. a function, script block, cmdlet etc) whenever the current path changes in the PowerShell console or ISE?
The scenario I am thinking of is to modify some environment variables and dot source some location-specific PowerShell functions depending on the current folder.
You have several options. You can remove the "cd" alias and then write a "cd" function and add the desired logic. The downside to this approach is that if someone uses Set-Location, your function is bypassed. Another option is to create a proxy command for Set-Location. Shay (and Kirk) have a video on how to do this here. The nice thing about this approach is that the built-in ways to change dir (cd and Set-Location) will go through your proxy command.

Please help me with a Power shell Script which rearranges Paths

I have both Sybase and MSFT SQL Servers installed. There is a time when Sybase interferes with MS SQL because they have they have some overlapping commands.
So, I need two scripts:
A) When runs, script A backs up the current path, grabs all paths that contain sybase or SYBASE or SyBASE (you get the point) in them and move them all at the very end of the path, while preserving the order.
B) When it runs, script B restores the path from back-up.
Both script a and script b should affect the path immediately. So, if a.bat that calls patha.ps1, pathb.ps1 looks like so:
#REM Old path here
call patha.ps1
#REM At this point the effective path should be different.
call pathb.ps1
#REM Effective old path again
Please let me know if this does not make sense. I am not sure if call command is the best one to use.
I have never used P.S. before. I can try to formulate the same thing in Python (I know S.O. users tend to ask for "What have you tried so far"). Well, at this point I am VERY slow at writing anything in Power Shell language.
Please help.
First of all: call will be of no use here as you are apparently writing a batch file and PowerShell scripts have no association to run them by default. call is for batch files or subroutines.
Secondly, any PowerShell script you call from a batch file cannot change environment variables of the caller's environment. That's a fundamental property of how processes behave and since you are calling another process, this is never going to work.
I'm not so sure why you are even using a batch file here in the first place if you have PowerShell. You might just as well solve this in PowerShell completely.
However, what I get from your problem is that the best way to resolve this is probably the following: Create two batch files that each set the PATH appropriately. You can probably leave out both the MSSQL and Sybase paths from your usual PATH and add them solely in the batch files. Then create shortcuts to
cmd /k set_mssql_path.cmd
and
cmd /k set_sybase_path.cmd
each of which now is a shortcut to a shell to work with the appropriate database's tools. This is how the Visual Studio Command Prompt works and it's probably the cleanest solution you have. You can use the color and prompt commands in those batches to make the two different shells distinct so you always know what environment you have. For example the following two lines will color the console white on blue and set a prompt indicating MSSQL:
color 1f
prompt MSSQL$S$P$G
This can be quite handy, actually.
Generally, trying to rearrange the PATH environment variable isn't exactly easy. While you could trivially split at a ; this will fail for paths that itself contain a semicolon (and which need to be quoted then). Even in PowerShell this will take a while to get right so I think creating shortcuts specific to the tools is probably the nicest way to deal with this.

How to add a script to the standard path in PowerShell?

how can i add a custom function / object to the standard array of recognized functions in PowerShell so that i can call it from the shell of PowerShell?
Thanks
You can put the function into your profile script. You can find out where this is by looking into the variable $profile. That script will be run automatically on starting Powershell (if you are allowed to run scripts) and functions declared in it will be available in every session.