This video discusses different places you may store a PowerShell function for use in multiple scripts. Learn how to create and use a profile script.
- Functions are probably the most useful building block of PowerShell Scripts. Building functions allows you to create features that are custom to your PowerShell environment, to your active directory, to your servers. I've talked about how to design a function in other courses. What I want to focus on here is where to keep your functions so that they'll be available when you need them. Profiles are an important part of an individual's PowerShell environment.
A PowerShell profile is a script that runs whenever you launch PowerShell. It's stored in the documents directory of a user's profile. They don't exist by default but, they're pretty easy to create. Here I am on my Windows 10 enterprise workstation. It's a member of the landonhotels.com domain and I'm currently logged in as the domain administrator. So I should have access to anything I need. Let me go ahead and launch Windows PowerShell as an administrator and let's test to see if we have a profile on this machine.
I'm going to use the Test-Path cmdlet because that's how you verify the existence of any path or file. And what I'm going to test for is the dollar sign profile. This variable exists throughout PowerShell and it points to a path and file name in the document's directory. When I run this, we'll find out that I don't currently have a profile script. Creating one is not much harder. I'm going to use the New-Item cmdlet.
With the New-Item cmdlet there are a couple of parameters that I need to specify. First, I need to say where I'm going to create something and I'm going to go ahead and use that profile path one more time and I need to specify what type of item I want to create. What I want to create is a file and finally I'm going to add the parameter of force. So that it will just get this done and not bother me with a lot of questions.
So there, it's complete. We have a file inside our document's library, in a folder named Windows PowerShell and there's the file name. I can open this in Notepad from here by simply typing Notepad and once again, the variable is the path and file name of that script. So clearly it was there because it's now open and we're looking at a blank profile script.
I could add now any functions that I would like to have to find every time I run PowerShell on my machine. I personally like having a Get-Time function available. The Get-Date cmdlet will give me the date and the time but, I'm going to create a function that will just return the time. So Function Get-Time and the open brace and the payload of this function will simply be the Get-Date cmdlet with the format option set to only give me hours and minutes.
And then I'll close the brace. Now I could include other things like a path statement that points to where I keep my scripts or modules or maybe aliases to cmdlets so that I can type things in the way I like. But this will serve our purposes for now. So let me go ahead and save the changes and close out of this script. To activate that, all I need to do is close PowerShell and reopen it.
And that script has now been run and I can run the Get-Time function to tell me what time it is. But remember where this PowerShell profile is stored. The location is significant, here in my document's directory that means it's user specific. If we don't have rolling profiles in our domain it's also machine specific. That's important to keep in mind when you write your functions. Profile scripts are a good place for the functions that customize your own personal PowerShell environment.
This may not be your best option as a place to store functions that you might want to call in your scripts. Scripts that might launch through a remote session or on the console of another server, things that you need to be able to see from someplace else. There are a couple of options that are more accessible. The first is placing the function in the script that actually needs it. Back here on our workstation, I can close out of this and let me open PowerShell ISE where I have a script loaded and a function that I've used in a previous course.
Notice that the function is defined at the beginning of the script where it's needed. PowerShell Scripts are always read and processed from the top down so functions need to be located at the top. Placing your functions in the scripts that call them is a common practice because it allows the script to be entirely self contained. All necessary functions are included. But it does have the limitation that this function is now only seen from inside this one script.
Sometimes a function is needed by multiple scripts. As we move forward we're going to take a look at a way to make functions available to multiple scripts on multiple systems within your network.
- Creating modules
- Using logic tests and loops in scripts
- Error handling
- Delegated administration
- Remote administration
- Creating background jobs
- Scheduling tasks and jobs