> For the complete documentation index, see [llms.txt](https://mohab-yehia.gitbook.io/sec505/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mohab-yehia.gitbook.io/sec505/welcome-to-securing-windows-with-powershell-a-deep-dive-into-sec505/sec-505.1/your-profile-script-s.md).

# Your Profile Script(s)

When you work in PowerShell, you might create custom functions, aliases, or variables to make your tasks easier. For example, you might create a shortcut (alias) for a long command or write a reusable function to automate a task. However, these customizations are **temporary**—they only exist in the current PowerShell session. If you close PowerShell and open it again, your custom functions, aliases, and variables are gone.

This is where **PowerShell profiles** come in. A profile is a script that runs automatically every time you start PowerShell. You can use this script to reload your custom functions, aliases, variables, and other settings so they’re always available when you need them.

#### **Why Use Profiles?**

Profiles are useful because:

1. **Persistence**: They allow you to save your customizations so they’re available in every new PowerShell session.
2. **Automation**: You can automate repetitive tasks by adding commands to your profile script.
3. **Customization**: You can tailor your PowerShell environment to your specific needs.

For example, if you frequently use a custom function or alias, you can add it to your profile so it’s always ready to use.

***

#### **Types of Profiles**

PowerShell supports multiple profile scripts, each with a different scope and purpose. These profiles are organized into four categories based on two factors:

1. **User Scope**:
   * **AllUsers**: Applies to all users on the system.
   * **CurrentUser**: Applies only to the currently logged-in user.
2. **Host Scope**:
   * **AllHosts**: Applies to all PowerShell hosts (e.g., the console and ISE).
   * **CurrentHost**: Applies only to the current host (e.g., just the console or just the ISE).

The four profile types are:

1. **`$profile.AllUsersAllHosts`**: Applies to all users and all hosts.
2. **`$profile.AllUsersCurrentHost`**: Applies to all users but only the current host.
3. **`$profile.CurrentUserAllHosts`**: Applies to the current user and all hosts.
4. **`$profile.CurrentUserCurrentHost`**: Applies to the current user and only the current host.

By default, the `$profile` variable points to `$profile.CurrentUserCurrentHost`.

***

#### **Profile Execution Order**

If multiple profile scripts exist, they are executed in the following order:

1. `$profile.AllUsersAllHosts`
2. `$profile.AllUsersCurrentHost`
3. `$profile.CurrentUserAllHosts`
4. `$profile.CurrentUserCurrentHost`

This means that settings in later scripts can override settings in earlier ones. For example, if you define an alias in `$profile.AllUsersAllHosts` and then redefine it in `$profile.CurrentUserCurrentHost`, the latter will take precedence.

***

#### **Creating and Editing Your Profile**

By default, profile scripts don’t exist. You need to create them manually. Here’s how:

**Step 1: Check if a Profile Exists**

You can check if a profile script already exists by running:

```
Test-Path $profile
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2FWtDiIfFAkZvaoDqZAxFv%2Fimage.png?alt=media&amp;token=029f38cd-d9c4-4778-a54c-cebe354cced9" alt=""><figcaption></figcaption></figure>

If this returns `False`, the profile doesn’t exist yet.

**Step 2: Create a Profile**

To create a profile script, use the following command:

powershellCopy

```
New-Item -Path $profile -ItemType File -Force
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2FOlhH5sq8KZTeA0N9pjIb%2Fimage.png?alt=media&amp;token=7c77b769-4ffe-4c75-8ca1-02303922e03e" alt=""><figcaption></figcaption></figure>

The `-Force` parameter ensures that the script is created even if the parent directory doesn’t exist.

**Step 3: Edit Your Profile**

Once the profile script is created, you can open it in your preferred editor. For example, to open it in the PowerShell ISE, run:

```
ise $profile
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2Fjf96fn35VxcEa1caltij%2Fimage.png?alt=media&amp;token=f45c7cd2-6a00-4666-8669-0941e1180eb0" alt=""><figcaption></figcaption></figure>

***

#### **What to Add to Your Profile**

You can add anything to your profile script that you want to run automatically when PowerShell starts. Here are some common examples:

**1. Custom Functions**

Define reusable functions in your profile. For example:

```
function Get-DiskUsage {
    Get-Volume | Select-Object DriveLetter, SizeRemaining, Size
}
```

Now, you can use `Get-DiskUsage` in any PowerShell session.

**2. Aliases**

Create shortcuts for frequently used commands. For example:

```
Set-Alias ll Get-ChildItem
```

Now, typing `ll` will run `Get-ChildItem`.

**3. Variables**

Set custom variables that you use often. For example:

```
$MyProjects = "C:\Projects"
```

Now, you can use `$MyProjects` to quickly navigate to your projects folder.

**4. Modules**

Import modules automatically. For example:

```
Import-Module PSReadLine
```

This ensures that the `PSReadLine` module is loaded in every session.

***

#### **Profile Scripts for Different Hosts**

PowerShell has different hosts, such as:

* **Console Host**: The default PowerShell terminal (`powershell.exe`).
* **ISE Host**: The PowerShell Integrated Scripting Environment (`powershell_ise.exe`).

Each host has its own profile script. For example:

* The console host uses: `Microsoft.PowerShell_profile.ps1`
* The ISE host uses: `Microsoft.PowerShellISE_profile.ps1`

If you want the same settings for both hosts, you can use the `$profile.CurrentUserAllHosts` script, which applies to all hosts.

***

#### **PowerShell Core Profiles**

If you’re using **PowerShell Core** (the cross-platform version of PowerShell), the profile paths are different from those in Windows PowerShell. For example:

* On Windows: `~\Documents\PowerShell\profile.ps1`
* On Linux/macOS: `~/.config/powershell/profile.ps1`

To ensure compatibility, always use the `$profile` variable instead of hardcoding paths.

***

#### **Example: A Simple Profile Script**

Here’s an example of what you might include in your profile script:

```
# Custom Functions
function Get-DiskUsage {
    Get-Volume | Select-Object DriveLetter, SizeRemaining, Size
}

# Aliases
Set-Alias ll Get-ChildItem

# Variables
$MyProjects = "C:\Projects"

# Import Modules
Import-Module PSReadLine

# Set Prompt Customization
function prompt {
    "PS [$env:USERNAME@$env:COMPUTERNAME] $PWD> "
}
```
