CDSoft.PowerShell/PowerShellHelper.ps1
function Get-PowerShellUserProfile { <# .SYNOPSIS ��ȡ PowerShell �� profile �ļ����ڡ��ĵ����ļ����У��� .DESCRIPTION ��ȡ PowerShell �� profile �ļ����ڡ��ĵ����ļ����У��� .PARAMETER PowerShellName shell �����ƣ���Ӧ���ĵ����ļ����е����ļ��С����� WindowsPowerShell �� PowerShell������ָ��ʱ�����Զ���⡣ .PARAMETER Create ���ļ�������ʱ���������� .EXAMPLE Get-PowerShellProfileFolders .OUTPUTS String[] #> [CmdletBinding()] PARAM( [string] $PowerShellName = '', [switch] $Create ) PROCESS { if (-not [String]::IsNullOrWhiteSpace($PowerShellName)) { $pwshType = $PowerShellName } elseif ($PSVersionTable.PSEdition -eq 'Desktop') { $pwshType = 'WindowsPowerShell' } elseif ($PSVersionTable.PSEdition -eq 'Core') { $pwshType = 'PowerShell' } else { throw 'Cannot determine WindowsPowerShell or PowerShell Core. Please check $PSVersionTable.PSEdition' } $doc = [Environment]::GetFolderPath("MyDocuments", 32768) $dir = Join-Path $doc $pwshType if ($Create -and -not (Test-Path $dir)) { New-Item $dir -ItemType Directory -Force } $myprofile = Join-Path $dir 'profile.ps1' New-Item $myprofile -ItemType File -ErrorAction SilentlyContinue return $myprofile } } function Get-PowerShellUserProfileFolders { <# .SYNOPSIS ��ȡ PowerShell �� profile Ŀ¼���ڡ��ĵ����ļ����У������ڿ��ܴ��ڶ�� profile Ŀ¼����˷���һ�����顣 .DESCRIPTION ��ȡ PowerShell �� profile Ŀ¼���ڡ��ĵ����ļ����У������ڿ��ܴ��ڶ�� profile Ŀ¼����˷���һ�����顣 .PARAMETER Create ��Ŀ¼������ʱ���������� .EXAMPLE Get-PowerShellProfileFolders .OUTPUTS String[] #> [CmdletBinding()] PARAM( [Switch] $Create ) PROCESS { $doc = [Environment]::GetFolderPath("MyDocuments", 32768) $ret = @() foreach ($child in 'PowerShell', 'WindowsPowerShell') { $dir = Join-Path $doc $child $inc = $false if (Test-Path $dir -PathType Container) { $inc = $true } elseif ($Create) { New-Item -Path $dir -ItemType Directory $inc = $true } if ($inc) { $ret += $dir } } return $ret } } function Get-PowerShellUserProfiles { <# .SYNOPSIS ��ȡ PowerShell �� profile �ļ����ڡ��ĵ����ļ����У������ڿ��ܴ��ڶ�� profile �ļ�����˷���һ�����顣 .DESCRIPTION ��ȡ PowerShell �� profile �ļ����ڡ��ĵ����ļ����У������ڿ��ܴ��ڶ�� profile �ļ�����˷���һ�����顣 .PARAMETER Create �� profile �ļ�������ʱ���������� .EXAMPLE Get-PowerShellUserProfiles .OUTPUTS String[] #> [CmdletBinding()] PARAM( [Switch] $Create ) PROCESS { $ret = @() $dirs = Get-PowerShellUserProfileFolders -Create:$Create foreach ($dir in $dirs) { $fn = Join-Path $dir 'profile.ps1' $inc = $false if (Test-Path $fn -PathType Leaf) { $inc = $true } elseif ($Create) { New-Item -Path $fn -ItemType File $inc = $true } if ($inc) { $ret += $fn } } return $ret } } |