close icon
PowerShell

Windows PowerShell Commands for Web Developers

Learn how Windows PowerShell makes your web development workflow more efficient.

April 30, 2019

Web development on Windows can be perceived by some developers as clunky due to the lack of proper native tooling. While Command Prompt, also known as cmd.exe, is less than ideal for an efficient development workflow, modern Windows systems come packed with the mighty Windows PowerShell, a command-line shell that includes an interactive prompt and a scripting environment.

PowerShell gives you access to the file system on the computer, lets you make server requests, and much more! However, guidance on running development tasks is often provided for MacOS and Linux only. To support Windows users, this guide demonstrates how to perform common web development tasks using PowerShell.

Use the navigation sidebar to jump to any section of your interest. Bookmark that section for future reference if you'd like!

Getting Help

At any point, if you need information about any PowerShell command or concept, you can use the Get-Help command to display this information in the console:

Get-Help
   [[-Name] <String>]
   [<CommonParameters>]

You can pass to Get-Help the command that you need information about. To make using Get-Help easier, you can use the help function which runs Get-Help internally but displays the result one page at a time.

Example

Display basic information about Get-Item one page at a time:

Get-Help Get-Item | Out-Host -Paging

To exit the manual, press q.

For a less complex command, you may use help:

help Get-Item

To get an example of the command usage, pass the -example flag:

help Get-Item -example

Pass the -online flag to open the PowerShell online document for the command:

help Get-Item -online

In the example above, the following URL opens in the default browser to show the Get-Item document page:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-item?view=powershell-6

Getting Your Current Location

To print information about your current working directory in the console, use Get-Location or gl.

Aliases

pwd, which is commonly used in UNIX-based operating systems.

Examples

This command prints your location in the current PowerShell drive.

PS C:\Users\User\Projects\powershell> Get-Location

Path
----
C:\Users\User\Projects\powershell


PS C:\Users\User\Projects\powershell> gl

Path
----
C:\Users\User\Projects\powershell

PS C:\Users\User\Projects\powershell> pwd

Path
----
C:\Users\User\Projects\powershell

Seeing Previous Activity

To get a list of the commands entered during the current session, use Get-History.

Get-History
   [[-Count] <Int32>]

This command takes a -Count or -c parameter to specify the number of most recent commands used that should be displayed.

Aliases

You can use the familiar UNIX command history as well as ghy and h.

Examples

Get the full history of the current session:

PS C:\Users\User\Projects\powershell> Get-History

  Id CommandLine
  -- -----------
   1 help Get-History -online
   2 cd .\Projects\
   3 ls
   4 cd .\powershell\
   5 ls
   6 rd .\test\
   7 rd .\test1\
   8 ls
   9 help Get-Location -Examples
  10 pwd
  11 help Get-History -Examples
  12 Get-History
  13 history 2

Get the two most recent entries of the current session:

PS C:\Users\User\Projects\powershell> history -c 2

  Id CommandLine
  -- -----------
  13 history 2
  14 Get-History

Printing the last command used

To print the last command used, execute $$:

PS C:\Users\User\Projects\powershell> $$
history

Printing to the Console

You can use Write-Output to print to the console.

Write-Output
     [-InputObject] <PSObject[]>

Aliases

You can use the more familiar echo command instead as well as write.

Examples

To get information about your home directory:

PS C:\Users\User\Projects\powershell> echo $HOME
C:\Users\User
PS C:\Users\User\Projects\powershell> write $HOME
C:\Users\User

Print a string:

PS C:\Users\User\Projects\powershell> echo "PowerShell is life"
PowerShell is life

Set a variable and print it:

PS C:\Users\User\Projects\powershell> $value = "Shell Life"
PS C:\Users\User\Projects\powershell> echo $value
Shell Life

Creating Multi-Line Commands

To make long commands easier to write, read, copy, and paste, you can spread them over multiple lines using the backtick (` ) as newline delimiter:

npm i @fortawesome/fontawesome-svg-core `
@fortawesome/free-solid-svg-icons `
@fortawesome/react-fontawesome

Copying and pasting this command in PowerShell will execute the three lines as a one-line command.

Viewing Content of Directories

To view the content of the current working directory, use the Get-ChildItem or gci command.

You can also pass it a directory path to see the content present at the end of the path.

Get-ChildItem
   [[-Path] <string[]>]

The -Path parameter is implied.

Aliases

You can also use the familiar UNIX ls command as well as the familiar cmd.exe command dir.

Examples

To view the content of the current directory:

PS C:\Users\User\Projects\powershell> gci
PS C:\Users\User\Projects\powershell>
# the directory is empty

To view the content of a nested directory, simply provide the path as an argument:

PS C:\Users\User\Projects\powershell> gci frameworks

If the frameworks subdirectory exists, the above command lets you see its content in the console.

Viewing hidden files

To view hidden files you add the -Force flag to Get-ChildItem or gci, or any of its aliases.

Viewing File Content

To output the content of a file right to the console, use the Get-Content or gc command.

Get-Content
   [-Path] <string[]>

The -Path parameter can be omitted.

Aliases

You can also use the familiar UNIX cat command as well as type.

Examples

Print the content of a package.json file present in the current directory:

PS C:\Users\User\Projects\powershell> gc .\package.json
{
  "name": "powershell",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Directory Operations

Creating a directory

  • The New-Item command can be used to create directories when given the right arguments.
New-Item
   [-Path] <String[]>
   -Name <String>
   [-ItemType <String>]

The -Path parameter receives as value the location path of the new item. If omitted, the current directory would be used as the default path.

The -Name parameter takes the name you want to give to the directory. You can omit it and simply pass a string at the end of the command.

The value of the -ItemType parameter is "directory". This parameter may also be simplified to -it.

Aliases

ni is the alias for New-Item.

Examples

Create a directory named frameworks under the current directory:

New-Item -ItemType "directory" frameworks

Shorthand version:

ni -it "directory" frameworks

Create two more directories, languages, and os, under the current directory. To create multiple directories, pass their path as a comma-separated list:

ni -it "directory" languages, os

Create a subdirectory whose parent exists:

PS C:\Users\User\Projects\powershell> ni -it "directory" languages/javascript


    Directory: C:\Users\User\Projects\powershell\languages


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/13/2019   4:12 PM                javascript

PS C:\Users\User\Projects\powershell> gci .\languages\


    Directory: C:\Users\User\Projects\powershell\languages


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/13/2019   4:12 PM                javascript

Assuming that the subdirectory employers doesn't exist, create a subdirectory named auth0 that would be its child:

PS C:\Users\User\Projects\powershell> ni -it "directory" employers/auth0


    Directory: C:\Users\User\Projects\powershell\employers


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/13/2019   4:14 PM                auth0


PS C:\Users\User\Projects\powershell> gci .\employers\


    Directory: C:\Users\User\Projects\powershell\employers


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/13/2019   4:14 PM                auth0

Additional Aliases

md and mkdir are also aliases for New-Item that already assume the ItemType to be "directory".

md [<Drive>:]<Path>
mkdir [<Drive>:]<Path>

The mkdir command is popularly used in tutorials online that target UNIX systems.

Deleting a directory

To delete any directory, use the Remove-Item command.

Remove-Item
      [-Path] <String[]>
      [<CommonParameters>]

As before, -Path is an optional parameter with the current directory being its default value.

Alias

You can use any of the following aliases with this command: ri, rm, rmdir, del, erase, and rd.

Examples

Remove the os subdirectory created earlier:

PS C:\Users\User\Projects\powershell> Remove-Item os

If you try to delete a directory that is not empty, instead of failing, PowerShell will warn you about it and give you the option to delete the directory recursively:

PS C:\Users\User\Projects\powershell> ri .\languages\

Confirm
The item at C:\Users\User\Projects\powershell\languages\ has children
and the Recurse parameter was not specified.
If you continue, all children will be removed with the item.
Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

It lets you choose from different options:

[Y] Yes
[A] Yes to All
[N] No
[L] No to All
[S] Suspend
[?] Help
(default is "Y"):

You can press enter to go with the Y default.

To delete a subdirectory, just provide the path.

Moving a directory

To move a directory you can use the Move-Item command. It moves the directory, including its properties, contents, and child items, from one location to another location.

Move-Item
    [-Path] <String[]>
    [[-Destination] <String>]

The -Path parameter specifies the path to the current location of the directory. If omitted, its default value is the current directory.

The -Destination parameter specifies the path to the location where the directory should be moved. When omitted, its default is the current directory.

Aliases

You can use the following aliases for this command: mi, move, and the common UNIX command, mv.

Examples

Create a subdirectory named angular under the current directory and move it to the frameworks sibling subdirectory:

PS C:\Users\User\Projects\powershell> ni -it "directory" angular


    Directory: C:\Users\User\Projects\powershell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/13/2019   4:36 PM                angular


PS C:\Users\User\Projects\powershell> mi .\angular\ .\frameworks\
PS C:\Users\User\Projects\powershell> gci


    Directory: C:\Users\User\Projects\powershell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/13/2019   4:14 PM                employers
d-----        3/13/2019   4:36 PM                frameworks

PS C:\Users\User\Projects\powershell> gci .\frameworks\


    Directory: C:\Users\User\Projects\powershell\frameworks


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/13/2019   4:36 PM                angular

Renaming directories

To rename a directory, you can use the same command to move a directory, Move-Item. You need to supply the same path to the directory but with a different name.

Examples

Rename the subdirectory angular inside the subdirectory frameworks to react:

PS C:\Users\User\Projects\powershell>
mi .\frameworks\angular\ .\frameworks\react
PS C:\Users\User\Projects\powershell>
gci .\frameworks\


    Directory: C:\Users\User\Projects\powershell\frameworks


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/13/2019   4:36 PM                react

Copying directories

To copy a directory from one location to another, use Copy-Item.

Copy-Item
    [-Path] <String[]>
    [[-Destination] <String>]

The -Path parameter specifies the path to the current location of the directory. If omitted, its default value is the current directory.

The -Destination parameter specifies the path to the location where the directory should be copied. When omitted, its default is the current directory.

Aliases

You can use cpi, copy, and the familiar UNIX cp as aliases for Copy-Item.

Examples

Create the spring subdirectory inside the frameworks subdirectory and then copy it to the employers directory:

PS C:\Users\User\Projects\powershell>
ni -it "directory" .\frameworks\spring


    Directory: C:\Users\User\Projects\powershell\frameworks


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/14/2019   1:51 PM                spring

PS C:\Users\User\Projects\powershell>
gci .\frameworks\


    Directory: C:\Users\User\Projects\powershell\frameworks


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/13/2019   4:36 PM                react
d-----        3/14/2019   1:51 PM                spring


PS C:\Users\User\Projects\powershell>
cpi .\frameworks\spring\ .\employers\
PS C:\Users\User\Projects\powershell>
gci .\employers\


    Directory: C:\Users\User\Projects\powershell\employers


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/13/2019   4:14 PM                auth0
d-----        3/14/2019   1:52 PM                spring

File Operations

The commands used to perform directory operations can be used to perform file operations as well.

Creating files

New-Item can be used to create a file. Be sure to provide the extension.

-ItemType can be omitted as its default value is to create a file.

New-Item
   [-Path] <String[]>
   -Name <String>
   [-ItemType <String>]

Aliases

ni is the alias for New-Item.

Examples

Create a file named index.js under the current directory

PS C:\Users\User\Projects\powershell>
ni index.js


    Directory: C:\Users\User\Projects\powershell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   1:56 PM              0 index.js


PS C:\Users\User\Projects\powershell>
gci


    Directory: C:\Users\User\Projects\powershell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/14/2019   1:52 PM                employers
d-----        3/14/2019   1:51 PM                frameworks
-a----        3/14/2019   1:56 PM              0 index.js

Create a file named main.java inside the frameworks/spring subdirectory:

PS C:\Users\User\Projects\powershell>
ni .\frameworks\spring\main.java


    Directory: C:\Users\User\Projects\powershell\frameworks\spring


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   1:58 PM              0 main.java


PS C:\Users\User\Projects\powershell>
gci .\frameworks\spring\


    Directory: C:\Users\User\Projects\powershell\frameworks\spring


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   1:58 PM              0 main.java

If the directory where you want to create the file doesn't exist, executing the following command throws an error:

PS C:\Users\User\Projects\powershell>
ni .\frameworks\django\main.py
ni : Could not find a part of the path
'C:\Users\User\Projects\powershell\frameworks\django\main.py'.
At line:1 char:1
+ ni .\frameworks\django\main.py
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\Users\User\P...\django\main.py:String) [New-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand

However, adding the -Force or -f parameter at the end lets you create both the directory and file within the same command:

PS C:\Users\User\Projects\powershell>
ni .\frameworks\django\main.py -f


    Directory: C:\Users\User\Projects\powershell\frameworks\django


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   2:00 PM              0 main.py


PS C:\Users\User\Projects\powershell>
gci .\frameworks\


    Directory: C:\Users\User\Projects\powershell\frameworks


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/14/2019   2:00 PM                django
d-----        3/13/2019   4:36 PM                react
d-----        3/14/2019   1:58 PM                spring

Creating multiple files

To create multiple files, the New-Item or ni command is used and paired with the -Path parameter. You can provide a comma-separated list of file paths as the value of -Path to create multiple files at once.

Examples

Create the files App.js, App.css, and index.css under the frameworks/react subdirectory:

PS C:\Users\User\Projects\powershell>
ni .\frameworks\react\App.js,`
.\frameworks\react\App.css,`
.\frameworks\react\index.css


    Directory: C:\Users\User\Projects\powershell\frameworks\react


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   2:02 PM              0 App.js
-a----        3/14/2019   2:02 PM              0 App.css
-a----        3/14/2019   2:02 PM              0 index.css

If any of the intermediate folders don't exist, you can use the -Force or -f parameter to create them:

If a command starts to get long, use the backtick (` ) to spread it into multiple lines

Deleting files

To delete files you can use the same command used to delete directories: Remove-Item.

Remove-Item
      [-Path] <String[]>
      [<CommonParameters>]

Aliases

You can use any of the following aliases with this command: ri, rm, del, erase, and rd.

Examples

Delete the main.java file present inside the frameworks/spring subdirectory:

PS C:\Users\User\Projects\powershell>
ri .\frameworks\spring\main.java
PS C:\Users\User\Projects\powershell>
gci .\frameworks\spring\
# empty subdirectory

Deleting multiple files

To delete multiple files, pass a list of comma-separated file paths to Remove-Item or ri.

Examples

PS C:\Users\User\Projects\powershell>
ri .\frameworks\react\App.css, .\frameworks\react\App.js
PS C:\Users\User\Projects\powershell>
gci .\frameworks\react\


    Directory: C:\Users\User\Projects\pwsh\frameworks\react


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/27/2019   7:43 PM              0 index.cs
-a----        3/27/2019   7:49 PM              0 index.css

It deletes the files but not the intermediate react folder.

Moving files

To move a file, you can use the same commands used to move directories: Move-Item. You just need to specify the filename in the path.

Move-Item
    [-Path] <String[]>
    [[-Destination] <String>]

Aliases

You can use the following aliases for this command: mi, move, and the common UNIX command, mv.

Examples

Move the index.js from the current directory to the frameworks/angular subdirectory:

PS C:\Users\User\Projects\powershell>
gci


    Directory: C:\Users\User\Projects\powershell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/14/2019   1:52 PM                employers
d-----        3/14/2019   2:09 PM                frameworks
-a----        3/14/2019   1:56 PM              0 index.js

PS C:\Users\User\Projects\powershell>
mi .\index.js .\frameworks\angular\
PS C:\Users\User\Projects\powershell>
gci


    Directory: C:\Users\User\Projects\powershell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/14/2019   1:52 PM                employers
d-----        3/14/2019   2:09 PM                frameworks

PS C:\Users\User\Projects\powershell>
gci .\frameworks\angular\


    Directory: C:\Users\User\Projects\powershell\frameworks\angular


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   1:56 PM              0 index.js

Renaming files

To rename a file, use the same command to move a directory, Move-Item. You need to supply the same path to the directory but with a different filename.

Examples

Rename the index.js file under the frameworks/angular subdirectory to index.ts:

PS C:\Users\User\Projects\powershell>
mv .\frameworks\angular\index.js .\frameworks\angular\index.ts
PS C:\Users\User\Projects\powershell>
gci .\frameworks\angular\


    Directory: C:\Users\User\Projects\powershell\frameworks\angular


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   1:56 PM              0 index.ts

Copying files

The same command used to copy directories is used to copy files: Copy-Item. The only difference is that now you must specify the filename at the end of the path:

Copy-Item
    [-Path] <String[]>
    [[-Destination] <String>]

The -Path parameter specifies the path to the current location of the file. If omitted, its default value is the current directory.

The -Destination parameter specifies the path to the location where the file should be copied. When omitted, its default is the current directory.

Aliases

You can use cpi, copy, and the familiar UNIX cp as aliases for Copy-Item.

Examples

Copy the file frameworks/react/index.css file to the subdirectory frameworks/angular/:

PS C:\Users\User\Projects\powershell>
gci .\frameworks\angular\


    Directory: C:\Users\User\Projects\powershell\frameworks\angular


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   1:56 PM              0 index.ts


PS C:\Users\User\Projects\powershell>
cpi .\frameworks\react\index.css .\frameworks\angular\
PS C:\Users\User\Projects\powershell>
gci .\frameworks\angular\


    Directory: C:\Users\User\Projects\powershell\frameworks\angular


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   2:02 PM              0 index.css
-a----        3/14/2019   1:56 PM              0 index.ts

Creating hidden files

To create a hidden file, create the file first and then add "Hidden" as one of its attributes.

Example

PS C:\Users\User\Projects\pwsh>
ni .env


    Directory: C:\Users\User\Projects\pwsh


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/27/2019   8:02 PM              0 .env

PS C:\Users\User\Projects\pwsh>
(gi .\.env).Attributes += 'Hidden'
PS C:\Users\User\Projects\pwsh>
gci


    Directory: C:\Users\User\Projects\pwsh


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/14/2019   5:28 PM                employers
d-----        3/14/2019   7:53 PM                frameworks
d-----        3/14/2019   5:18 PM                languages

(gi .\.env) returns the .env file as an object. Then, you can change its Attributes property. Remember that gi is the shorthand for Get-Item.

"Learn how to create, delete, copy, and move files and directories using Windows PowerShell."

Tweet

Tweet This

To move to a subdirectory, you can use the Set-Location or sl command. However, using the cd alias is more natural and universal among technical documentation.

cd works just like in UNIX. Pass as an argument to this command the path where you want to go:

PS C:\Users\User> cd .\Projects\
PS C:\Users\User\Projects>
# takes you to the Projects subdirectory
PS C:\Users\User\Projects> cd .\powershell\frameworks\
PS C:\Users\User\Projects\powershell\frameworks>
 # takes you two folders down to the frameworks subdirectory
PS C:\Users\User\Projects\powershell\frameworks> cd ..
PS C:\Users\User\Projects\powershell>
# takes you back one level up to the powershell subdirectory
PS C:\Users\User\Projects\powershell>  cd ../..
PS C:\Users\User>
 # takes you two levels up to the User directory
PS C:\Users\User> cd .\Projects\powershell\frameworks\angular\
PS C:\Users\User\Projects\powershell\frameworks\angular> cd ~
PS C:\Users\User>
# cd ~ takes you to the defined home directory

Combining Commands

To combine commands use a semicolon (;).

Create a new hardware subdirectory and make it the current directory:

PS C:\Users\User> cd .\Projects\powershell\
PS C:\Users\User\Projects\powershell>
ni -it "directory" hardware; cd hardware


    Directory: C:\Users\User\Projects\powershell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        3/14/2019   2:32 PM                hardware


PS C:\Users\User\Projects\powershell\hardware>

It's important to note that the second command will run even if the first one fails. Assume that the hardware folder exists but the colleges folder doesn't. The following commands attempts to create a new file within each of these folders:

PS C:\Users\User\Projects\powershell>
ni colleges\mit.html; ni hardware\macbook.html

ni : Could not find a part of the path
'C:\Users\User\Projects\powershell\colleges\mit.html'.
At line:1 char:1
+ ni colleges\mit.html; ni hardware\macbook.html
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\Users\User\P...lleges\mit.html:String) [New-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand



    Directory: C:\Users\User\Projects\powershell\hardware


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   2:35 PM              0 macbook.html

Since the colleges folder doesn't exist, the first command, ni colleges\mit.html, fails and throws an error in the console. However, the second command, ni hardware\macbook.html, executes successfully and creates the file macbook.html within the hardware folder.

This is the same exact behavior that & has in cmd.exe.

Finding a File in a Directory

You can also use Get-ChildItem or gci to search if a file is present in the given directory.

Examples

PS C:\Users\User\Projects\powershell>
gci App.css

If App.css is present in the current working directory, you'll see information about the file printed on the screen. Otherwise, you'll see an error like this one:

gci : Cannot find path 'C:\Users\User\Projects\powershell\App.css' because it does not exist.
At line:1 char:1
+ gci App.css
+ ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\User\P...ershell\App.css:String) [Get-ChildItem], ItemNotFoundExcep
   tion
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

To search within the current directory and subdirectories for the file, you need to pass along the -Recurse and -Force parameters:

PS C:\Users\User\Projects\powershell>
gci App.css -Force -Recurse

If the file is present in the tree below the current directory, its information is printed on the console, which includes its locations:

    Directory: C:\Users\User\Projects\powershell\frameworks\angular


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   2:02 PM              0 App.css


    Directory: C:\Users\User\Projects\powershell\frameworks\react


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/14/2019   2:02 PM              0 App.css

Adding Content to a File

To add content to a file, use Add-Content. PowerShell commands are very declarative.

Add-Content
  [-Path] <string[]>
  [-Value] <Object[]>

The -Path parameter specifies the path to the items that receive the content. You can omit it and pass the path as a value to Add-Content directly.

The -Value parameter specifies the content to be added. If passing a string, you can omit it and simply provide the content to add as a quoted string after the path value. This will be clear in the examples.

Aliases

You can also use ac instead.

Examples

Add a the ".auth0 {}" string to the App.css file present in frameworks/react:

PS C:\Users\User\Projects\powershell>
gc .\frameworks\react\App.css
PS C:\Users\User\Projects\powershell>
ac .\frameworks\react\App.css ".auth0 {}"
PS C:\Users\User\Projects\powershell>
gc .\frameworks\react\App.css
.auth0 {}

Finding Text Within Files

Use Select-String or sls to find files that include certain text.

Select-String
      [-Pattern] <string[]>

You need to pipe files to Select-String.

The -Pattern parameter specifies the text to find on each file line. You can use a string or a regular expression as its value. However, if you pass it a string, use the SimpleMatch parameter.

Examples

To search for the occurrence of the "auth0" string within the current directory and its subdirectories, use the following:

PS C:\Users\User\Projects\powershell>
gci -Recurse | Select-String -Pattern "auth0"

auth0
frameworks\react\App.css:1:.auth0 {}

As you learned before, gci -Recurse is used to traverse the current directory and subdirectories. That output is then passed to Select-String for it to detect within all those files an occurrence of the string "auth0". When the operation is complete, you'll see printed in the console a list of the files with their location and the line that contains the pattern.

Comparing Two Files

To compare the contents of two files and determine if they are identical in content, you can use the Compare-Object command.

Compare-Object
       [-ReferenceObject] <PSObject[]>
       [-DifferenceObject] <PSObject[]>

Compare-Object takes as arguments the reference object and the difference object.

Aliases

You can also use diff and compare as aliases.

Examples

Use Compare-Object to find if two files are identical:

Compare-Object `
-ReferenceObject $(Get-Content package.json) `
-DifferenceObject $(Get-Content clone.json)

If the files differ, you'll see the following output:

InputObject        SideIndicator
-----------        -------------
  "name": "react", =>
  "name": "node",  <=

If they are identical, there's no output.

You have to execute the Get-Content command for each file, as Compare-Object compares two sets of objects. For files, Get-Content reads one line at a time and then returns a collection of objects, each of which represents a line of content. These object collections are then used by Compare-Object to determine any differences in content.

To execute commands within commands, you can use the following syntax:

$(<command to execute>)

For example, if you want to print to the screen what Get-Content returns, run the following:

echo $(Get-Content package.json)

Marking Server Requests

Probably the most commonly used commands in tech tutorials online are curl and wget. These commands are used to get content from a web page online or make requests to servers. Fortunately, PowerShell offers an equivalent command to achieve this task: Invoke-WebRequest.

Invoke-WebRequest
      [-UseBasicParsing]
      [-Uri] <Uri>

This command is very powerful and can do a lot. You'll learn how to use it to make basic requests to a server.

Aliases

To make your life easier, Invoke-WebRequest has wget and curl as aliases. You can also use iwr.

Examples

Let's say that you have a Node Express web server running on localhost:8080. The server has an /api/users endpoint that handles GET and POST requests.

To make a GET request, use the following command:

PS C:\Users\User>
curl "http://localhost:8080/api/users"


StatusCode        : 200
StatusDescription : OK
Content           : ["Tony","Lisa","Michael","Ginger","Alice"]
RawContent        : HTTP/1.1 200 OK
                    Connection: keep-alive
                    Content-Length: 42
                    Content-Type: application/json; charset=utf-8
                    Date: Thu, 14 Mar 2019 22:14:50 GMT
                    ETag: W/"2a-X4GgAs2DaZWDhw7cuMCCGMiGj58"
                    X-Powered-By:...
Forms             : {}
Headers           : {[Connection, keep-alive], [Content-Length, 42], [Content-Type, application/json; charset=utf-8],
                    [Date, Thu, 14 Mar 2019 22:14:50 GMT]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : System.__ComObject
RawContentLength  : 42

If you do a curl, you may get the following error:

curl : The response content cannot be parsed because the Internet Explorer
engine is not available, or Internet Explorer's
first-launch configuration is not complete.
Specify the UseBasicParsing parameter and try again.

This means that Internet Explorer has not been launched for the first time in your Windows system. To solve it, launch Internet Explorer and choose to use the recommended settings.

Internet Explorer 11 recommended security and compatibility settings

To make a POST request, you can first define the body of the request using a variable named $Body:

PS C:\Users\User> $Body = @{
>>     name = 'alice'
>>     lastName = 'jones'
>> }

To create the object, type $Body = @{ and then press <ENTER> to define its properties as shown above.

Then you can use this $Body variable with the -Body parameter to make the POST request as follows:

PS C:\Users\User>
curl "http://localhost:8080/api/users" -Method "POST" -Body $Body


StatusCode        : 200
StatusDescription : OK
Content           : OK
RawContent        : HTTP/1.1 200 OK
                    Connection: keep-alive
                    Content-Length: 2
                    Content-Type: text/plain; charset=utf-8
                    Date: Thu, 14 Mar 2019 22:31:34 GMT
                    ETag: W/"2-nOO9QiTIwXgNtWtBJezz8kv3SLc"
                    X-Powered-By: Express...
Forms             : {}
Headers           : {[Connection, keep-alive], [Content-Length, 2], [Content-Type, text/plain; charset=utf-8], [Date, Thu,
                    14 Mar 2019 22:31:34 GMT]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : System.__ComObject
RawContentLength  : 2

The -Method parameter specifies the HTTP method used for the web request. It can accept any of the HTTP verbs.

The -Body parameter specifies the body of the request.

"Learn how to make server requests using Windows PowerShell!"

Tweet

Tweet This

Checking the Integrity of Downloaded Packages

Go to https://www.jetbrains.com/webstorm/download/download-thanks.html and download the WebStorm installer. You'll use it as an example of how to verify the integrity of a file.

In the download page, JetBrains suggests to:

Download and verify the file SHA-256 checksum.

Clicking on the SHA-256 checksum link opens a basic page that shows the following output:

62196e17f1965c97d2c32bf30fadaf7935168933de6ecf23483acfc8bd4a90c6 *WebStorm-2018.3.5.exe

This checksum can help you determine that the file you downloaded hasn't been tampered by anyone during transmission and it's the file JetBrains intended for you to download.

Once the file is downloaded, you can make the Downloads folder your current directory and issue the following command to compute the hash of the downloaded file:

certutil -hashfile .\WebStorm-2018.3.5.exe SHA256

Certutil.exe is a CLI tool used to verify certificates, key pairs, and certificate chains.

Upon completion, the command prints the following to the screen:

SHA256 hash of .\WebStorm-2018.3.5.exe:
62196e17f1965c97d2c32bf30fadaf7935168933de6ecf23483acfc8bd4a90c6
CertUtil: -hashfile command completed successfully.

You can then compare this output with the checksum provided by JetBrains on their site. If the file name and the hash match perfectly, you can be assured that you downloaded the right file.

Running Commands as Administrator

You can manually start PowerShell as administrator by right-clicking on its desktop entry and choosing "Run as Administrator" or selecting that from its right menu options. That action opens a console that has elevated access and permissions.

PowerShell launch options - run as Administrator

However, you can also start an elevated console through a command in PowerShell:

Start-Process powershell -Verb runAs <process>

This command starts a process with the runAs verb, which in turn starts the process with permissions of a system admin.

For example, to start PowerShell as an administrator you can run the following command:

Start-Process -FilePath "powershell" -Verb RunAs

Using Shell File Editors

If you are feeling adventurous, you can always download Vim for Windows and use it inside PowerShell.

Further Reading

Getting Started with Windows PowerShell

Understanding important PowerShell concepts

PowerShell Microsoft.PowerShell.Core module providers that manage the basic features of PowerShell

Recap

As a Windows developer, PowerShell provides you with the same capabilities that any terminal application for MacOS and Linux have.

Are there any other tasks that this guide should include or tasks that could be run better with different syntax or commands? Please let me know in the comments below if you have any insights to share! I am loving using PowerShell while making cool web apps.

About Auth0

Auth0 by Okta takes a modern approach to customer identity and enables organizations to provide secure access to any application, for any user. Auth0 is a highly customizable platform that is as simple as development teams want, and as flexible as they need. Safeguarding billions of login transactions each month, Auth0 delivers convenience, privacy, and security so customers can focus on innovation. For more information, visit https://auth0.com.

  • Twitter icon
  • LinkedIn icon
  • Faceboook icon