CDSoftware/Shortcut.ps1
function New-Shortcut { <# .SYNOPSIS ����һ����ݷ�ʽ�� .DESCRIPTION ����һ����ݷ�ʽ�� .PARAMETER TargetPath ��ݷ�ʽָ����ļ���Ŀ¼·���� .PARAMETER Directory ��ݷ�ʽ���ڵ�Ŀ¼�� .PARAMETER Name ��ݷ�ʽ�ļ����������ָ������Ϊ��ָ����ļ�����������չ������ָ���Ŀ¼���� .PARAMETER Arguments �����в����� .PARAMETER IconLocation ͼ��λ�á� .EXAMPLE New-Shortcut -TargetPath D:\v2ray\v2rayN.exe -Directory $env:APPDATA\Microsoft\Windows\StartMenu\Programs -Name v2ray #> [CmdletBinding(SupportsShouldProcess)] PARAM( [parameter(Mandatory=$true, ValueFromPipeline=$true)] [String] $TargetPath, [parameter(Mandatory=$true)] [String] $Directory, [String] $Name, [String] $Arguments, [String] $IconLocation ) PROCESS { if (Test-Path $TargetPath -PathType Leaf) { $file = Get-Item $TargetPath $workDir = $file.Directory if ('' -eq $Name) { $Name = $file.BaseName } } elseif (Test-Path $TargetPath -PathType Container) { $dir = Get-Item $TargetPath $workDir = $dir.Directory if ('' -eq $Name) { $Name = $dir.BaseName } } else { throw "ָ����·�� $TargetPath �����ڡ�" } $lnkFn = Join-Path $Directory "$Name.lnk" if ((Test-Path $lnkFn) -and -not ($PSCmdlet.ShouldProcess("Overwriting file $lnkFn", $lnkFn, "Overwriting"))) { return } $wshShell = New-Object -ComObject WScript.Shell $shortcut = $wshShell.CreateShortcut($lnkFn) $shortcut.TargetPath = $TargetPath $shortcut.WorkingDirectory = $workDir if ('' -ne $Arguments) { $shortcut.Arguments = $Arguments } if ('' -ne $IconLocation) { $shortcut.IconLocation = $IconLocation } $shortcut.Save() Write-Verbose "��Ϊ $TargetPath ������ݷ�ʽ��λ��Ϊ $lnkFn" } } function New-StartupItem { <# .SYNOPSIS ���Խ�һ���������Ϊϵͳ���� .DESCRIPTION ���Խ�һ���������Ϊϵͳ���� .PARAMETER TargetPath ����·���� .PARAMETER Arguments �����в����� .EXAMPLE New-StartupItem -TargetPath D:\v2ray\v2rayN.exe .OUTPUTS void #> [CmdletBinding()] PARAM( [parameter(Mandatory=$true, ValueFromPipeline=$true)] [String] $TargetPath, [String] $Arguments ) PROCESS { if (-not ($TargetPath.EndsWith('.exe'))) { throw "����ָ���ļ����� .exe ��β�ij���" } $dir = "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\Startup" New-Shortcut -TargetPath $TargetPath -Directory $dir -Arguments $Arguments -WhatIf:$WhatIfPreference Write-Verbose "��Ϊ $TargetPath ��������" } } function New-StartScreenItem { <# .SYNOPSIS ���Խ�һ��������ӵ�����ʼ��Ļ���� .DESCRIPTION ���Խ�һ��������ӵ�����ʼ��Ļ���� .PARAMETER TargetPath ����·���� .PARAMETER Arguments �����в����� .EXAMPLE New-StartScreenItem -TargetPath D:\v2ray\v2rayN.exe .OUTPUTS void #> [CmdletBinding()] PARAM( [parameter(Mandatory=$true, ValueFromPipeline=$true)] [String] $TargetPath, [String] $Arguments ) PROCESS { throw "��������ʱ�����á�" if (-not ($TargetPath.EndsWith('.exe'))) { throw "����ָ���ļ����� .exe ��β�ij���" } $dir = "$env:APPDATA\Microsoft\Windows\Start Menu\Programs" New-Shortcut -TargetPath $TargetPath -Directory $dir -Arguments $Arguments -WhatIf:$WhatIfPreference Write-Verbose "��Ϊ $TargetPath ��������" } } |