Skip to content

Set-ScriptFileRequirement

SYNOPSIS

Sets correct module requirements for PowerShell scripts, ignoring local functions, [Alias()] attributes, Set-Alias-based aliases, and the '.' or '&' operators in the same folder.

SYNTAX

Set-ScriptFileRequirement [-Path] <String> [-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm]
 [<CommonParameters>]

DESCRIPTION

This function can process either a single .ps1 file or an entire folder (recursively). It uses two phases:

Phase 1 (Collection): - Parse each file to gather local function names, [Alias("...")] attributes, and Set-Alias definitions.

Phase 2 (Analysis): - Parse each file again to find commands that need external modules. - Skips: * Locally defined functions * Aliases that map to local functions * Module paths that reside in the same folder * Special operators '.' and '&' - Inserts #Requires lines for any truly external modules. - Appends #FIX: comments for commands that are not resolved.

EXAMPLES

EXAMPLE 1

Set-ScriptFileRequirement -Path "C:\MyScripts" -Verbose
Recursively scans C:\MyScripts, updates #Requires lines in each .ps1 file,
and provides verbose output.

EXAMPLE 2

Set-ScriptFileRequirement -Path "./Scripts/Deploy.ps1" -Debug
Processes only the Deploy.ps1 file, displaying debug messages with internal
processing details.

PARAMETERS

-Path

A path to either a single .ps1 file or a folder.

Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Confirm

Prompts you for confirmation before running the cmdlet.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-ProgressAction

{{ Fill ProgressAction Description }}

Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

System.Void

NOTES

  • Operators '.' (dot-sourcing) and '&' (call operator) are explicitly ignored, since they are not actual commands that map to modules.