powershell MailboxTransfer.ps1

MailboxTransfer.ps1

MailboxTransfer.ps1
# Creates remote powershell session to an Exchange server and prompts for credentials
$Session = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri http://myrs-msexmbx-01.ci.charlotte.nc.us/PowerShell/ -AllowRedirection -Authentication Kerberos -Credential (Get-Credential)
Import-PSSession $Session 
# Allows the script to run against the entire AD forest
Set-ADServerSettings -ViewEntireForest $true
# If an error occurs, this shows the error before closing the window
If ($? -ne "True") {Pause; Exit}

# Start of script
Write-Host "================ Enter Information ================" 
$mailboxname = Read-Host "Enter EMAIL ADDRESS, ALIAS, or PRINCIPAL NAME OF ACTIVE ACCOUNT"
Write-Host "================ Results ================"
Get-Mailbox $mailboxname | fl name,alias,servername
Get-MailboxStatistics -identity $mailboxname | fl database,servername,mailboxguid,legacydn,EmailAddresses
# Outputs the command to a text file for retention purposes
Get-MailboxStatistics -identity $mailboxname | fl database,servername,mailboxguid,legacydn,EmailAddresses | Out-File mailboxtransferdetails.txt
Get-Mailbox $mailboxname | fl EmailAddresses
$connectmailboxidentity = Get-Mailbox $mailboxname | ft -HideTableHeaders DisplayName | Out-String | ForEach-Object { $_.Trim() }
$mailboxGUID = Get-MailboxStatistics -Identity $mailboxname | ft -HideTableHeaders mailboxguid | Out-String | ForEach-Object { $_.Trim() }
$mailboxDATABASE = Get-MailboxStatistics -Identity $mailboxname | ft -HideTableHeaders database | Out-String | ForEach-Object { $_.Trim() }
If ($? -ne "True") {Pause; Exit}
Pause
Write-Host "======= Give this command to a member of the Exchange Team to run AFTER Disabling the mailbox =======" 
$sentence = "Update-StoreMailboxState -Identity " + $mailboxGUID + " -Database " + $mailboxDATABASE 
Write-Output $sentence 
If ($? -ne "True") {Pause; Exit}
Pause

# Disabling mailbox action
Write-Host "================ Disabling Mailbox ================"
Disable-Mailbox -identity $mailboxname
If ($? -ne "True") {Pause; Exit}
Write-Host "================ Mailbox Disabled ================="


# Connecting mailbox action
Write-Host "========= Connect Mailbox to new account =========="
$newadaccount = Read-Host "Enter new AD account UPN@DOMAIN"
$newemailaddress = Read-Host "Enter new email Alias"
Write-Host "================ Connecting Mailbox ==============="
Connect-Mailbox -Identity $connectmailboxidentity -User $newadaccount -Database $mailboxDATABASE -Alias $newemailaddress
If ($? -ne "True") {Pause; Exit}
Write-Host "================ Mailbox Connected ================"
Write-Output "Mailbox Transer completed, please allow 24 hours for mail address policy to be in effect"
Pause

powershell 关数のドキュメント

document
    <#
     .synopsis
      概要
     .description
      詳細な説明
     .PARAMETER パラメーター名
      パラメーター(複数可)。パラメーターの前のコメントよりもこちらが優先します。
     .notes
      メモ
    #>

powershell 基本功能模板

base_function.ps1
#region FunctionTemplate
    function FunctionTemplate {
        [CmdletBinding()]
        PARAM(
            [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
            [object]
            $InputObject
        )

        begin {
            Write-Verbose $MyInvocation.MyCommand
        }

        process {
            try {
                
            }
            catch {
                Throw $_
            }
        }

        end {
        }
    }
#endregion FunctionTemplate

powershell PowerShell Codex

Rename Multiple Files
# SOURCE: https://stackoverflow.com/a/29078913/3436775
Dir | ren -NewName { $_.Name -replace "Adhesive Wombat - ", "" }

powershell 使用PowerShell以递归方式删除Windows中所有子文件夹中的`node_modules`文件夹,以避免超出路径max_length

使用PowerShell以递归方式删除Windows中所有子文件夹中的`node_modules`文件夹,以避免超出路径max_length

remove_node_modules_recursively.ps1
<# 
Note: Eliminate `-WhatIf` parameter to get action be actually done 
Note: PS with version prior to 4.0 can't delete non-empty folders
#>

Get-ChildItem -Path "." -Include "node_modules" -Recurse -File:$false | Remove-Item -Recurse -Force -WhatIf

powershell test.ps1

test.ps1
choco install spotify slack steam greenshot nodejs docker-desktop vscode sublimetext3 notepadplusplus gitkraken visualstudio2017community octave blender magicavoxelviewer -y

powershell 授予O365日历访问权限

GetMBoxFolderPerms.ps1
Get-MailboxFolderPermission user@@domain.tld:\calendar
AddMboxFolderPermissions.ps1
Add-MailboxFolderPermission -Identity user:\calendar -user user@domain.tld -AccessRights Editor
RemoveMboxFolderPermissions.ps1
Remove-MailboxFolderPermission -Identity user@domain.tld:\calendar –user user@domain.tld

powershell Choco Python安装

巧克力安装的python在windows上

install_python
#from admin console window
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
choco install python

#now setup for cf-lint
python -m pip install --upgrade pip
pip install cfn-lint

powershell Active Directory用户名/ Pre2k以显示名称

Snip_AD_Username_To_DisplayName
([adsisearcher]"(&(objectClass=user)(samaccountname=$env:username))").FindOne().Properties['displayname'] | Out-String

powershell taskschedule

ps5
# スクリプト
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe

# 引数
-Command ".\test.ps1"

# 開始(実行時のワークディレクトリ) ダブルクォート不要
C:\test\
ps6

# スクリプト
"C:\Program Files\PowerShell\6.XXX.XXX\pwsh.exe"

# 引数