PowerShell/docs/new-script.md
2025-05-12 22:04:02 +02:00

2.0 KiB

The new-script.ps1 Script

This PowerShell script creates a new PowerShell script file by using the template file at: ../data/templates/PowerShell.ps1.

Parameters

/Repos/PowerShell/scripts/new-script.ps1 [[-path] <String>] [<CommonParameters>]

-path <String>
    Specifies the path and new filename
    
    Required?                    false
    Position?                    1
    Default value                
    Accept pipeline input?       false
    Aliases                      
    Accept wildcard characters?  false

[<CommonParameters>]
    This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, 
    WarningVariable, OutBuffer, PipelineVariable, and OutVariable.

Example

PS> ./new-script.ps1 bot.ps1
 New PowerShell script 'bot.ps1' created from template 'PowerShell.ps1'.

Notes

Author: Markus Fleschutz | License: CC0

https://github.com/fleschutz/PowerShell

Script Content

<#
.SYNOPSIS
	Creates a new PowerShell script
.DESCRIPTION
	This PowerShell script creates a new PowerShell script file by using the template file at: ../data/templates/PowerShell.ps1.
.PARAMETER path
	Specifies the path and new filename
.EXAMPLE
	PS> ./new-script.ps1 bot.ps1
	✅ New PowerShell script 'bot.ps1' created from template 'PowerShell.ps1'.
.LINK
	https://github.com/fleschutz/PowerShell
.NOTES
	Author: Markus Fleschutz | License: CC0
#>

param([string]$path = "")

try {
	if ($path -eq "" ) { $path = Read-Host "Enter the new filename" }

	$pathToTemplate = Resolve-Path "$PSScriptRoot/../data/templates/PowerShell.ps1" 
	Copy-Item $pathToTemplate "$path"
	if ($lastExitCode -ne 0) { throw "Can't copy to: $path" }

	"✅ New PowerShell script '$path' created from template 'PowerShell.ps1'."
	exit 0 # success
} catch {
	"⚠️ Error: $($Error[0])"
	exit 1
}

(page generated by convert-ps2md.ps1 as of 05/12/2025 22:02:56)