- How to create and run a PowerShell script file on Windows 10
- How to create PowerShell script file on Windows 10
- Creating script with Visual Studio Code
- Install Visual Studio Code
- Install PowerShell extension
- Create PowerShell script with Visual Studio Code
- Creating script with Notepad
- Creating script with Integrated Scripting Environment
- How to run PowerShell script file on Windows 10
- More Windows 10 resources
- Halo: MCC’s live service elements make it better, not worse
- Microsoft’s Surface Duo is not ‘failing up’
- Here’s what you can do if Windows 10 update KB5001330 is causing issues
- These are the best PC sticks when you’re on the move
- New-Item
- Syntax
- Description
- Examples
- Example 1: Create a file in the current directory
- Example 2: Create a directory
- Example 3: Create a profile
- Example 4: Create a directory in a different directory
- Example 5: Create multiple files
- Example 6: Use wildcards to create files in multiple directories
- Example 7: Create a symbolic link to a file or folder
- Example 8: Use the -Force parameter to attempt to recreate folders
- Example 9: Use the -Force parameter to overwrite existing files
- Parameters
- Inputs
- Outputs
- Notes
How to create and run a PowerShell script file on Windows 10
Source: Windows Central
On Windows 10, PowerShell is a command-line tool designed by Microsoft to run commands and scripts to change settings and automate tasks. In a way, it’s similar to Command Prompt. However, PowerShell is a more capable command-line interface (CLI) that offers an extensive set of tools and more flexibility and control. Also, unlike Command Prompt, PowerShell is available on Windows, macOS, and Linux.
A script is just a collection of commands saved into a text file (using the special «.ps1» extension) that PowerShell understands and executes in sequence to perform different actions.
The only caveat is that the default security protocol always blocks any script from running on a device. This means that when double-clicking a «.ps1» file on Windows 10 nothing will happen, and if you try to run the script within PowerShell, you’ll see the «cannot be loaded because running scripts is disabled on this system» error message. However, it’s not impossible to run scripts on your computer. You only need to enable the correct execution policy.
In this Windows 10 guide, we’ll walk you through the steps to successfully write and run your first script file on PowerShell using Visual Studio Code, Notepad, and the PowerShell Integrated Scripting Environment (ISE) console.
How to create PowerShell script file on Windows 10
On Windows 10, you can create PowerShell script files using virtually any text editor or the ISE console. However, the preferred option (thanks @jotaka for the heads up) to build scripts moving forward is to use the Visual Studio Code editor with the PowerShell extension.
Creating script with Visual Studio Code
Visual Studio Code — also known as VS Code — is a free and extensible cross-platform code editor that provides an environment to edit virtually any kind of programming language. And when adding the PowerShell extension, you get a fully interactive scripting editing experience, even with IntelliSense (code-completion) support.
The new experience is meant to be the new default, but the PowerShell ISE console isn’t going away. Still, the company won’t be adding any more features, and it doesn’t support PowerShell 7 or higher releases.
Install Visual Studio Code
To install Visual Basic Code on Windows 10, use these steps:
Click the Windows button to download the installer.
Source: Windows Central
Click the Next button.
Source: Windows Central
Confirm additional tasks as necessary.
Source: Windows Central
Once you complete the steps, you can proceed to install the PowerShell extension.
Install PowerShell extension
To install the PowerShell extension on VS Code, use these steps:
- Open VS Code.
- Click the Extensions tab from the left pane.
- Search for PowerShell and select the top result.
Click the Install button.
Source: Windows Central
After you complete the steps, you can start writing PowerShell scripts using Visual Studio Code.
Create PowerShell script with Visual Studio Code
To create a script with Visual Basic Code, use these steps:
- Open VS Code.
Click the File menu and select the New File option.
Source: Windows Central
Click the File menu and select the Save as option.
Source: Windows Central
Write a new, or paste the script you want to run — for example:
Write-Host «Congratulations! Your first script executed successfully»
The above script will output the phrase «Congratulations! Your first script executed successfully» on the screen.
(Optional) Click the Run button from the top-right side (or press the F5 key) to run the script.
Source: Windows Central
Creating script with Notepad
To create a PowerShell script using the Notepad editor on Windows 10, use these steps:
- Open Start.
- Search for Notepad, and click the top result to open the app.
Write a new, or paste your script, in the text file — for example:
Write-Host «Congratulations! Your first script executed successfully»
Source: Windows Central
Type a descriptive name for the script — for example, first_script.ps1.
Source: Windows Central
Creating script with Integrated Scripting Environment
Alternatively, you can use the built-in PowerShell ISE console to code your scripts on Windows 10.
The Integrated Scripting Environment is an advanced tool, but you can get started using these steps:
- Open Start.
- Search for Windows PowerShell ISE, right-click the top result, and select the Run as administrator option.
- Click on File menu.
Select the New option to create a new empty .ps1 file.
Source: Windows Central
Write a new, or paste the script you want to run — for example:
Write-Host «Congratulations! Your first script executed successfully»
Source: Windows Central
Type a name for the script – for example, first_script.ps1.
Source: Windows Central
Once you complete the steps using Notepad, Visual Studio Code, or PowerShell ISE, the script will be ready to run, but it will fail by default. This is because the default PowerShell settings are always set to block the execution of any script. (The only exception is if you run the contents of the script within Visual Studio Code or PowerShell ISE.)
How to run PowerShell script file on Windows 10
If you wish to run a script file with PowerShell, you have to change the execution policy on Windows 10.
To change the execution policy to run PowerShell scripts, use these steps:
- Open Start.
- Search for PowerShell, right-click the top result, and select the Run as administrator option.
Type the following command to allow scripts to run and press Enter:
Type A and press Enter (if applicable).
Source: Windows Central
Type the following command to run the script and press Enter:
In the above command, make sure to change «PATH\TO\SCRIPT» to the location of your script.
For example, this command runs a script stored in the Downloads folder:
Source: Windows Central
After you complete the steps, the script will run, and if it was written correctly, you should see its output without issues.
On Windows 10, PowerShell includes four execution policies, including:
- Restricted — Stops any script from running.
- RemoteSigned — Allows scripts created on the device, but scripts created on another computer won’t run unless they include a trusted publisher’s signature.
- AllSigned — All the scripts will run, but only if a trusted publisher has signed them.
- Unrestricted — Runs any script without any restrictions.
In the above steps, we use the command to allow local scripts to run on Windows 10. However, if you’re not planning to run scripts regularly, you can restore the default settings to block untrusted scripts using the same instructions outlined above, but on step No. 4, make sure to use the Set-ExecutionPolicy Restricted command.
More Windows 10 resources
For more helpful articles, coverage, and answers to common questions about Windows 10, visit the following resources:
Halo: MCC’s live service elements make it better, not worse
Halo: The Master Chief Collection is more popular than ever, but some fans don’t agree with the live service approach 343 Industries has taken with it. Here’s why those elements are, at the end of the day, great for the game and for Halo overall.
Microsoft’s Surface Duo is not ‘failing up’
Microsoft announced this week that it was expanding Surface Duo availability to nine new commercial markets. While Surface Duo is undoubtedly a work in progress, this is not a sign of a disaster. It’s also doesn’t mean that Surface Duo is selling a ton either. Instead, the reason for the expansion is a lot more straightforward.
Here’s what you can do if Windows 10 update KB5001330 is causing issues
In this guide, we’ll show you the steps to get rid of the update KB5001330 to fix profile, gaming, and BSoD problems with the Windows 10 October 2020 Update and May 2020 Update.
These are the best PC sticks when you’re on the move
Instant computer — just add a screen. That’s the general idea behind the ultra-portable PC, but it can be hard to know which one you want. Relax, we have you covered!
New-Item
Creates a new item.
Syntax
Description
The New-Item cmdlet creates a new item and sets its value. The types of items that can be created depend on the location of the item. For example, in the file system, New-Item creates files and folders. In the registry, New-Item creates registry keys and entries.
New-Item can also set the value of the items that it creates. For example, when it creates a new file, New-Item can add initial content to the file.
Examples
Example 1: Create a file in the current directory
This command creates a text file that is named «testfile1.txt» in the current directory. The dot (‘.’) in the value of the Path parameter indicates the current directory. The quoted text that follows the Value parameter is added to the file as content.
Example 2: Create a directory
This command creates a directory named «Logfiles» in the C: drive. The ItemType parameter specifies that the new item is a directory, not a file or other file system object.
Example 3: Create a profile
This command creates a PowerShell profile in the path that is specified by the $profile variable.
You can use profiles to customize PowerShell. $profile is an automatic (built-in) variable that stores the path and file name of the «CurrentUser/CurrentHost» profile. By default, the profile does not exist, even though PowerShell stores a path and file name for it.
In this command, the $profile variable represents the path of the file. ItemType parameter specifies that the command creates a file. The Force parameter lets you create a file in the profile path, even when the directories in the path do not exist.
After you create a profile, you can enter aliases, functions, and scripts in the profile to customize your shell.
Example 4: Create a directory in a different directory
This example creates a new Scripts directory in the «C:\PS-Test» directory.
The name of the new directory item, «Scripts», is included in the value of Path parameter, instead of being specified in the value of Name. As indicated by the syntax, either command form is valid.
Example 5: Create multiple files
This example creates files in two different directories. Because Path takes multiple strings, you can use it to create multiple items.
Example 6: Use wildcards to create files in multiple directories
The New-Item cmdlet supports wildcards in the Path parameter. The following command creates a temp.txt file in all of the directories specified by the wildcards in the Path parameter.
The Get-ChildItem cmdlet shows three directories under the C:\Temp directory. Using wildcards the New-Item cmdlet creates a temp.txt file in all of the directories under the current directory. The New-Item cmdlet outputs the items you created, which is piped to Select-Object to verify the paths of the newly created files.
Example 7: Create a symbolic link to a file or folder
This example creates a symbolic link to the Notice.txt file in the current folder.
In this example, Target is an alias for the Value parameter. The target of the symbolic link can be a relative path. Prior to PowerShell v6.2, the target must be a fully-qualified path.
Beginning in PowerShell 7.1, you can now create to a SymbolicLink to a folder on Windows using a relative path.
Example 8: Use the -Force parameter to attempt to recreate folders
This example creates a folder with a file inside. Then, attempts to create the same folder using -Force . It will not overwrite the folder but simply return the existing folder object with the file created intact.
Example 9: Use the -Force parameter to overwrite existing files
This example creates a file with a value and then recreates the file using -Force . This overwrites The existing file and it will lose it’s content as you can see by the length property
When using New-Item with the -Force switch to create registry keys, the command will behave the same as when overwriting a file. If the registry key already exists, the key and all properties and values will be overwritten with an empty registry key.
Parameters
Prompts you for confirmation before running the cmdlet.
Type: | SwitchParameter |
Aliases: | cf |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
This parameter is not supported by any providers installed with PowerShell. To impersonate another user or elevate your credentials when running this cmdlet, use Invoke-Command .
Type: | PSCredential |
Position: | Named |
Default value: | Current user |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Forces this cmdlet to create an item that writes over an existing read-only item. Implementation varies from provider to provider. Even using the Force parameter, the cmdlet cannot override security restrictions.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies the provider-specified type of the new item. The available values of this parameter depend on the current provider you are using.
If your location is in a FileSystem drive, the following values are allowed:
Creating a SymbolicLink type on Windows requires elevation as administrator. However, Windows 10 (build 14972 or newer) with Developer Mode enabled no longer requires elevation creating symbolic links.
In a Certificate drive, these are the values you can specify:
- Certificate Provider
- Certificate
- Store
- StoreLocation
For more information see about_Providers.
Type: | String |
Aliases: | Type |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specifies the name of the new item. You can specify the name of the new item in the Name or Path parameter value, and you can specify the path of the new item in Name or Path value. Items names passed using the Name parameter are created relative to the value of the Path parameter.
Type: | String |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specifies the path of the location of the new item. The default is the current location when Path is omitted. You can specify the name of the new item in Name, or include it in Path. Items names passed using the Name parameter are created relative to the value of the Path parameter.
For this cmdlet, the Path parameter works like the LiteralPath parameter of other cmdlets. Wildcard characters are not interpreted. All characters are passed to the location’s provider. The provider may not support all characters. For example, you cannot create a filename that contains an asterisk ( * ) character.
Type: | String [ ] |
Position: | 0 |
Default value: | Current location |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specifies the value of the new item. You can also pipe a value to New-Item .
Type: | Object |
Aliases: | Target |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Shows what would happen if the cmdlet runs. The cmdlet is not run.
Type: | SwitchParameter |
Aliases: | wi |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
You can pipe a value for the new item to this cmdlet.
Outputs
This cmdlet returns the item that it creates.
Notes
New-Item is designed to work with the data exposed by any provider. To list the providers available in your session, type Get-PsProvider . For more information, see about_Providers.