Powershell
Powershell Modules
Installing
Find the path of your modules by typing:
$Env:PSModulePath
Listing
Get-Module -ListAvailable
Importing
Import-module -name ModuleName
Copy a file to the specified directory
List certain things about directories...
This does a
Get-ChildItem
in directory.
then pipes to this
| ? { $_.PSIsContainer }
the
?
is short forWhere-Object
and{ $_.PSIsContainer }
displays only directories
then pipes to
| sort LastWriteTime
| sort LastWriteTime
sorts by when the directories were saved last
then pipes to
| select name,lastwritetime
| select name,lastwritetime
displays it in the 2 columns ofname
andlastwritetime
Another Example
Get help for commands
Listing Symbolic Links and Targets
Download files with Powershell
Save output to file
Ammed a file with the -Amend
flag
Write a Powershell Script (for Aliases)
Simple. For example...
Open a Windows PowerShell window and type:
Then create a function, such as:
Then type this under the function name:
Now you can type the word "google" into Windows PowerShell and have it execute the code within your function!
Another Example of writing a Powershell Script (for Aliases)
You will have to create a function first, that has your command in it. Then create an alias to that function.
This is using the Powershell command line:
This is inside the $profile
file:
Not quite sure what I pasted below this line but someday I'll check it out.
PowerShell’s Path Environmental Variable
On this page I will show you how to view, and how to change the Path variable using PowerShell commands.
One benefit of the path variable is less typing; if you type just the name of an executable, the operating system locates the program by searching through the values stored in this variable.
Topics for Windows PowerShell’s Env:Path
List $Env:Path with PowerShell
You can also see path values in the Control Panel; navigate to the System section and then click on the link to ‘Advanced system settings’. Our purpose is employing PowerShell to list these paths.
Remember that we are dealing with an Environmental Variable, hence **$**Env.
Note 1: You really do need that $dollar sign. Plain Env:Path does not work.
Note 2: Observe a semi-colon between each item; this is valuable information if you need to append more Path values.
See here for a refresher on PowerShell’s Environmental Variables’ drive.
[Environment] Method Here is an alternative method which lists the path values, but employs the base .Net Framework elements.
Note 3: My point is to plant the idea that you could modify the “Path” with the sister command SetEnvironmentalVariable.
Problem Changing Environment Variable Values with PowerShell
When you change the value of an environment variable using PowerShell commands, the changes only affect the current session. This behavior mimics using the Set command of previous Windows operating systems.
You can use PowerShell to make a persistent change, the technique involves making changes the registry values. Firstly, we will display the Environment values in the registry, then we will append another location.
I have also seen suggestions for putting SetEnvironmentalVariable commands in the profile files, Microsoft.PowerShell_profile, Microsoft.PowerShellISE_profile or profile.ps1.
Retrieving Path Info from the Registry
The solution to the temporary nature of PowerShell’s changes to the environmental variable values is to script persistent registry modifications. This is the equivalent of making changes to the Advanced system settings in the Control Panel.
Permanently Modifying the Env:Path
Note 4: The key command is Set-ItemProperty.
Note 5: Remember that you need a semi-colon to separate the values.
System Properties GUI
Firstly, if there is a GUI that corresponds to my PowerShell script, then I like to examine its menus to check that my script is working, and to give me ideas to improve my code. The screenshot below is taken from the Control Panel.
The registry script (above) achieves the same result as pressing the ‘Edit…’ button in the Advanced tab of the System Properties.
Function Add-Path
Note 5: My function has a built-in value for $NewPath, you may wish to change “D:\PowerShell” to the value required by your project; for example: Add-Path -NewPath “C:\Project”
Further Research on Env:Path
Here are ideas to discover more about Environmental Variables.
**Env:PathExt **In addition to Env:Path, there is a variable called Env:PathExt
**Get-Member **In this instance, Get-Member provides more methods than properties.
**Help About_Environment_Variables **For find out more about Environmental Variables, PowerShell provides this Help About… file.
Note 6: This shows that PowerShell considers Env: as a drive, similar to regular file system drives such as C:\.
**List Environmental Variables **Change the location to the Env: drive and then call for GCI (Get-ChildItem).
There are other ways of listing the environmental variables, for example: Get-Item Env:
See more about PowerShell’s Environmental Variables »
Summary of PowerShell’s Env:Path
If you wish to add locations to the path environmental variable then you can use PowerShell rather than the GUI in the Control Panel. If you type just the name of an executable, the operating system locates the program by searching through the values stored in the Path Variable.
Last updated