Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I alias the dot sourcing command?

Tags:

powershell

I find myself missing the . in dot sourced files and spending a little time getting my footing when switching from C# to PS. Is there a way to alias the . or use a similar command that is more blatantly noticeable?

like image 436
Tom Padilla Avatar asked Feb 17 '20 13:02

Tom Padilla


People also ask

How to use dot source in PowerShell?

To dot source a script, type a dot (.) and a space before the script path. After the UtilityFunctions. ps1 script runs, the functions and variables that the script creates are added to the current scope.

What is Dot sourcing a function?

Dot-sourcing is a concept in PowerShell that allows you to reference code defined in one script in a separate one. This is useful when you're working on a project with multiple scripts and might have functions specified in one script(s) and the code to call those functions in another.

What does dot do in PowerShell?

The PowerShell dot-source operator brings script files into the current session scope. It is a way to reuse script. All script functions and variables defined in the script file become part of the script it is dot sourced into. It is like copying and pasting text from the script file directly into your script.

Is dot sourcing a script the right way to go?

However, when thinking about dot sourcing a script, look into creating a module instead. Both solve the same problem, but a module allows more flexibility and easier management. However, if all that's needed is a quick way to bring one script into another script's scope, dot sourcing is the way to go.

What is dot-sourcing in PowerShell?

Dot-sourcing is a concept in PowerShell that allows you to reference code defined in one script in a separate one. This is useful when you're working on a project with multiple scripts and might have functions specified in one script(s) and the code to call those functions in another.

What is dot-sourcing and how do I do it?

When a need arises to add an additional "thing" that's when it's time to add another script or function to your project. Dot-sourcing is a way to do just that. Dot-sourcing is a concept in PowerShell that allows you to reference code defined in one script in a separate one.

How do i dot source a script?

To dot source a script is incredibly simple. I'll now dot source my functions script inside of my DoStuff.ps1 script. If my Functions.ps1 script were located on the root of my C drive, I just put a period, followed by a space and then the path of the script I'd like to dot source.


Video Answer


1 Answers

In short: No, you cannot define an alias for :

., the (dot-)sourcing operator, is an operator and as such it cannot be the target of an alias (only commands can).

Defining a function is also not an option, because it would itself have to be dot-sourced when called, in order to be able to dot-source other commands (scripts whose definitions to load into the caller's scope).

like image 171
mklement0 Avatar answered Oct 03 '22 21:10

mklement0