Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the different PowerShell file types?

If I try to save a file in ISE, the default extension is Powershell Files with parenthesis (*.ps1, *.psm1, *.psd1, *.ps1xml, *.pssc, *.psrc, *.cdxml) and with descriptions

Powershell Scripts (*.ps1)
Powershell Modules (*.psm1)
Powershell Data Files (*.psd1)
Powershell Session Configuration Files (*.pssc)
Powershell Role Capability Files (*.psrc)
Powershell Xml Files (*.psxml, *.cdxml)

What do each of these do?

like image 499
Nikola Johnson Avatar asked Jun 27 '20 00:06

Nikola Johnson


People also ask

What file type does PowerShell use?

PowerShell scripts have a . ps1 file extension. Running a script is a lot like running a cmdlet. You type the path and file name of the script and use parameters to submit data and set options.

What are ps1 files?

A PS1 file is a script, or "cmdlet," used by Windows PowerShell, a Windows shell program built on Microsoft's . NET Framework. It contains a series of commands written in the PowerShell scripting language. PS1 files are similar to .

What are the default file extensions associated with PowerShell?

Windows PowerShell default file extension associations. .msh2xml.ps1.ps1xml.ps2.ps2xml.psc1.psc2.psd1.psm1.

What is the difference between PowerShell scripts and PowerShell modules?

.ps1 files are PowerShell scripts; it is the most common type of PowerShell file and one that is the most like other shell scripts like .sh, .bat files .psm1 files are PowerShell modules; these can contain multiple function scripts interacted as a package, can import modules into PowerShell with the Import-Module cmdlet as well as Install-Module

Do different versions of PowerShell work?

I've recently used Powershell, but there appear to be several different versions of this utility. Some of them work--some of them merely report not being able to find something or call up something else. For instance, when I call up Powershell from an elevated command prompt, Powershell does not let me use a script to retrieve the chkdsk log.

What are the different types of PowerShell values?

Types In PowerShell, each value has a type, and types fall into one of two main categories: value types and reference types. Consider the type int, which is typical of value types.


1 Answers

  • .ps1 files are PowerShell scripts; it is the most common type of PowerShell file and one that is the most like other shell scripts like .sh, .bat files

  • .psm1 files are PowerShell modules; these can contain multiple function scripts interacted as a package, can import modules into PowerShell with the Import-Module cmdlet as well as Install-Module

    • Other Module-Interaction cmdlets:
      • Export-ModuleMember
      • Find-Module
      • Get-InstalledModule
      • Get-Module
      • ImportSystemModules
      • New-Module
      • New-ModuleManifest
      • etc, tab through *Module* in PowerShell
  • .psd1 files are PowerShell data files or PowerShell module manifest files; they provide information and instructions and serve as manifest files for psm1 files/PowerShell modules; stores configuration data. Can serve as help file for PowerShell modules. For more information, see https://learn.microsoft.com/en-us/powershell/scripting/developer/module/how-to-write-a-powershell-module-manifest?view=powershell-7. Manipulated with Import-PowerShellDataFile, New-ModuleManifest, Test-ModuleManifest, Update-ModuleManifest, and Import-LocalizedData cmdlets.

  • .ps1xml files are PowerShell XMLs for PowerShell modules, similar to .psd1 files but in XML format. A module manifest is also required for modules that support the Updatable Help feature. Updatable Help uses the HelpInfoUri key in the module manifest to find the Help information (HelpInfo XML) file that contains the location of the updated help files for the module. PowerShell XML files can serve as the help file for a module: Requires the existence of a module manifest file to do this. There are certain names for ps1xml files that are designated to do certain things, see https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_types.ps1xml?view=powershell-7 and https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_format.ps1xml?view=powershell-7

  • .pssc files are PowerShell session configuration files; Describes session configuration files, which are used in a session configuration (also known as an "endpoint") to define the environment of sessions that use the session configuration. Session configuration files make it easy to create custom session configurations without using complex C# assemblies or scripts. .pssc files are in hashtable format and contain the configuration information for a PowerShell session. See https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_session_configuration_files?view=powershell-7. .pssc files use the Register-PSSessionConfiguration, Set-PSSessionConfiguration, and New-PSSessionConfigurationFile cmdlets.

  • .psrc files are PowerShell role capability files; they define a set of capabilities that are used in session configuration and session configuration (pssc) files. Uses hashtable format. Uses the New-PSRoleCapabilityFile cmdlet. See https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/new-psrolecapabilityfile?view=powershell-7

  • .cdxml files are PowerShell XML files that create cmdlets using CIM tools. See https://learn.microsoft.com/en-us/previous-versions/windows/desktop/wmi_v2/cdxml-overview

Examples:

Example psd1:

@{

RootModule = 'ExampleModule.psm1'
ModuleVersion = '0.0.1'
GUID = '1234abcd-1234-abcd-xxxx-000000000000'
Author = 'User01'
CompanyName = 'Unknown'
Copyright = '(c) User01. All rights reserved.'
Description = 'Example module'

Example types.ps1xml:

<Type>
  <Name>System.Object[]</Name>
  <Members>
    <AliasProperty>
      <Name>Test</Name>
      <ReferencedMemberName>
        Length
      </ReferencedMemberName>
    </AliasProperty>
  </Members>
</Type>

Adds new member Count to System.Arrays with member type AliasProperty:

gm -InputObject @(1,2,3,4)| ?{$_.Name -eq "test"}


   TypeName: System.Object[]

Name       MemberType    Definition
----       ----------    ----------
Test       AliasProperty Test = Length

Example psrc:

New-PSRoleCapabilityFile `
    -Path ".\Test.psrc" `
    -Author "TestUser" `
    -CompanyName "Test Company" `
    -Description "Description" `
    -ModulesToImport "Microsoft.PowerShell.Core"

Creates Test.psrc with parameters
Test.psrc content:

@{

# ID used to uniquely identify this document
GUID = '4fb9608e-474c-44c3-a752-ea1d9b27dcb0'

# Author of this document
Author = 'TestUser'

# Description of the functionality provided by these settings
Description = 'Description'

# Company associated with this document
CompanyName = 'Test Company'

# Copyright statement for this document
Copyright = '(c) 2020 TestUser. All rights reserved.'

# Modules to import when applied to a session
ModulesToImport = 'Microsoft.PowerShell.Core'

# Aliases to make visible when applied to a session
# VisibleAliases = 'Item1', 'Item2'

# Cmdlets to make visible when applied to a session
# VisibleCmdlets = 'Invoke-Cmdlet1', @{ Name = 'Invoke-Cmdlet2'; Parameters = @{ Name = 'Parameter1'; ValidateSet = 'Item1', 'Item2' }, @{ Name = 'Parameter2'; ValidatePattern = 'L*' } }

# Functions to make visible when applied to a session
# VisibleFunctions = 'Invoke-Function1', @{ Name = 'Invoke-Function2'; Parameters = @{ Name = 'Parameter1'; ValidateSet = 'Item1', 'Item2' }, @{ Name = 'Parameter2'; ValidatePattern = 'L*' } }

# External commands (scripts and applications) to make visible when applied to a session
# VisibleExternalCommands = 'Item1', 'Item2'

# Providers to make visible when applied to a session
# VisibleProviders = 'Item1', 'Item2'

# Scripts to run when applied to a session
# ScriptsToProcess = 'C:\ConfigData\InitScript1.ps1', 'C:\ConfigData\InitScript2.ps1'

# Aliases to be defined when applied to a session
# AliasDefinitions = @{ Name = 'Alias1'; Value = 'Invoke-Alias1'}, @{ Name = 'Alias2'; Value = 'Invoke-Alias2'}

# Functions to define when applied to a session
# FunctionDefinitions = @{ Name = 'MyFunction'; ScriptBlock = { param($MyInput) $MyInput } }

# Variables to define when applied to a session
# VariableDefinitions = @{ Name = 'Variable1'; Value = { 'Dynamic' + 'InitialValue' } }, @{ Name = 'Variable2'; Value = 'StaticInitialValue' }

# Environment variables to define when applied to a session
# EnvironmentVariables = @{ Variable1 = 'Value1'; Variable2 = 'Value2' }


# Format files (.ps1xml) to load when applied to a session
# FormatsToProcess = 'C:\ConfigData\MyFormats.ps1xml', 'C:\ConfigData\OtherFormats.ps1xml'

# Assemblies to load when applied to a session
# AssembliesToLoad = 'System.Web', 'System.OtherAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

}

Example pssc

New-PSSessionConfigurationFile -RoleDefinitions
@{ 'User' = @{ RoleCapabilities = 'Maintenance' }}
-Path .\Maintenance.pssc

Creates Maintenance.pssc with content

@{

# Version number of the schema used for this document
SchemaVersion = '2.0.0.0'

# ID used to uniquely identify this document
GUID = 'a4f7e8eb-f787-4f2c-bf0f-555db2ca2b05'

# Author of this document
Author = 'User'

# Description of the functionality provided by these settings
# Description = ''

# Session type defaults to apply for this session configuration. Can be 'RestrictedRemoteServer' (recommended), 'Empty', or 'Default'
SessionType = 'Default'

# Directory to place session transcripts for this session configuration
# TranscriptDirectory = 'C:\Transcripts\'

# Whether to run this session configuration as the machine's (virtual) administrator account
# RunAsVirtualAccount = $true

# Scripts to run when applied to a session
# ScriptsToProcess = 'C:\ConfigData\InitScript1.ps1', 'C:\ConfigData\InitScript2.ps1'

# User roles (security groups), and the role capabilities that should be applied to them when applied to a session
RoleDefinitions = @{
    'User' = @{
        'RoleCapabilities' = 'Maintenance' } }

}

Register file with

Register-PSSessionConfiguration -Name Name -Path .\Maintenance.pssc

Now PowerShell sessions started with Name use Maintenance.pssc. To start a PowerShell session with Name use

New-PSSession -ConfigurationName Name
like image 133
Nico Nekoru Avatar answered Nov 15 '22 05:11

Nico Nekoru