• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
OpenTechTips

OpenTechTips

Comprehensive IT Guides for Pros and Enthusiasts

MENUMENU
  • HOME
  • ALL TOPICS
    • Exchange
    • InfoSec
    • Linux
    • Networking
    • Scripting
      • PowerShell
    • SSL
    • Tools
    • Virtualization
    • Web
    • Windows
  • ABOUT
  • SUBSCRIBE
Home » How to Create and Use Custom Powershell Commads

How to Create and Use Custom Powershell Commads

April 20, 2020 - by Zsolt Agoston - last edited on May 5, 2020

Custom commands

John needs to access Exchange PowerShell a lot. He's tired of logging in the actual Exchange server all the time, he prefers to use his local computer to run cmdlets and manage mailboxes.

He created a script, called "mail.ps1" to import all the Exchange cmdlets in his local machine, but he wants to be able to run the mail command simply, whenever he opens a PowerShell window. All without browsing to his personal folder and use the .ps1 extension.

Also, he shares the client machine with a few other administrators, and he wants all the other guys to be able to use the mail command as well.

1. Utilize the "$Profile script" to create a function for himself1

Powershell has a special file in the actual user's Documents folder, that automatically runs when powershell starts. By default it is not there, it needs to be manually created.

Then we can add any commands, create functions, anything that we need.

PowerShell even has a pre-set variable for it, called $PROFILE. We can check where it is stored and what the file is called.

PS C:\> $PROFILE C:\Users\jdoe\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

However, it does not exist for the users by default. He creates it with a simple command, then opens it and adds his contents.

PS C:\> $PROFILE C:\Users\jdoe\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 PS C:\> PS C:\> New-Item -Force -Type File $PROFILE Directory: C:\Users\jdoe\Documents\WindowsPowerShell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 20/04/2020 14:46 0 Microsoft.PowerShell_profile.ps1 PS C:\>notepad $PROFILE

function mail {
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://mail.alwayshotcafe.com/powershell/" -Authentication kerberos Import-PSSession $ExchangeSession
}

He can now open a new PowerShell window, and simply use the mail command to connect to Exchange.

How to Create and Use Custom Powershell Commads

2. Allow Everyone to use the custom commands

Besides having $PROFILE that resolves to $HOME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1, there is another useful location, a script that runs for everyone who opens a PS window on the local machine.

Just use $PROFILE.AllUsersAllHosts instead of $PROFILE. Or edit it using it's fully qualified location: C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

PS C:\> $PROFILE C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 PS C:\> PS C:\> New-Item -Force -Type File $PROFILE.AllUsersAllHosts Directory: C:\Users\jdoe\Documents\WindowsPowerShell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 20/04/2020 14:46 0 Microsoft.PowerShell_profile.ps1 PS C:\>notepad $PROFILE.AllUsersAllHosts

How to Create and Use Custom Powershell Commads

 

 

Then test that the command runs in any user profiles

How to Create and Use Custom Powershell Commads

Reader Interactions

Comments Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tools

Secondary Sidebar

CONTENTS

  • Custom commands
  • 1. Utilize the “$Profile script” to create a function for himself1
  • 2. Allow Everyone to use the custom commands

  • Terms of Use
  • Disclaimer
  • Privacy Policy
Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}