powershell 你好Scoop

对于勺子测试代码

hello_scoop.ps1
$name = split-path (whoami) -leaf
"Hi, $name! Welcome to Scoop."

powershell 清理Imsim安装(Windows)

ImsimCleanup.ps1
<#
    Script to clean all of the Stadlerrail Imsim Apps and data stored and used by it
#>

# Get all of the application installed with Vendor parameter equals 'Stadlerrail'
Write-Information "Collecting data for installed application ..."
$stadlerrailApps = Get-WmiObject -Class Win32_Product -Filter "Vendor = 'Stadlerrail'"

if ($stadlerrailApps.Length -eq 0) {
  Write-Warning "There are no application installed. If you disagree - you should remove it manually"
}

# Uninstall in loop
foreach ($app in $stadlerrailApps) {
  Write-Information "Removing $($app.Name) ..."
  $app.Uninstall()
}

# Get default storage path
$appLocalStorage = Join-Path -Path $env:USERPROFILE -ChildPath ".imsim"
Write-Information "Default local storage path is [$appLocalStorage]. Checking if it exists ..."

# Check if folder exists and remove it
if (Test-Path $appLocalStorage) {
  $message = "Found [$appLocalStorage]."
  $question = 'Are you sure you want to remove it?'
  $choices  = '&Yes', '&No'
  $decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
  if ($decision -eq 0) {
    Write-Information "Removing local storage ..."
    Remove-Item -Path $appLocalStorage -Recurse -Force
  } else {
    Write-Information "Oki-Doki"
  }  
} else {
  Write-Warning "No local storage found. Guess its OK"
}

Pause

powershell 安装Chrome

install_chrome
$P = $env:TEMP + '\chrome_installer.exe'; Invoke-WebRequest 'https://dl.google.com/chrome/install/latest/chrome_installer.exe' -OutFile $P; Start-Process -FilePath $P -Args '/silent /install' -Verb RunAs -Wait; Remove-Item $P

powershell 将用户从每用户MFA转换为基于条件访问的MFA

https://docs.microsoft.com/en-us/azure/active-directory/authentication/howto-mfa-getstarted#convert-users-from-per-user-mfa-to-conditional-access-based-mfa

convertMFA.ps1
# Disable MFA for all users, keeping their MFA methods intact
Get-MsolUser -All | Disable-MFA -KeepMethods

# Enforce MFA for all users
Get-MsolUser -All | Set-MfaState -State Enforced

# Wrapper to disable MFA with the option to keep the MFA
# methods (to avoid having to proof-up again later)
function Disable-MFA {

    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$True)]
        $User,
        [switch] $KeepMethods
    )

    Process {

        Write-Verbose ("Disabling MFA for user '{0}'" -f $User.UserPrincipalName)
        $User | Set-MfaState -State Disabled

        if ($KeepMethods) {
            # Restore the MFA methods which got cleared when disabling MFA
            Set-MsolUser -ObjectId $User.ObjectId `
                         -StrongAuthenticationMethods $User.StrongAuthenticationMethods
        }
    }
}

# Sets the MFA requirement state
function Set-MfaState {

    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipelineByPropertyName=$True)]
        $ObjectId,
        [Parameter(ValueFromPipelineByPropertyName=$True)]
        $UserPrincipalName,
        [ValidateSet("Disabled","Enabled","Enforced")]
        $State
    )

    Process {
        Write-Verbose ("Setting MFA state for user '{0}' to '{1}'." -f $ObjectId, $State)
        $Requirements = @()
        if ($State -ne "Disabled") {
            $Requirement =
                [Microsoft.Online.Administration.StrongAuthenticationRequirement]::new()
            $Requirement.RelyingParty = "*"
            $Requirement.State = $State
            $Requirements += $Requirement
        }

        Set-MsolUser -ObjectId $ObjectId -UserPrincipalName $UserPrincipalName `
                     -StrongAuthenticationRequirements $Requirements
    }
}

powershell PowerShell的下设置环境变量

env.ps
#Powershell设置环境变量

#查看所有环境变量  
ls env:

#搜索环境变量   
ls env:NODE*

#查看单个环境变量 
$env:NODE_ENV

#添加/更新环境变量 
$env:NODE_ENV=development

#删除环境变量        
del evn:NODE_ENV

powershell PowerShell的自定义提示,在切片栏中显示您当前的Az订阅

PowerShell的自定义提示,在切片栏中显示您当前的Az订阅

CustomPowerShellProfilePrompt.ps1
function Prompt {

    $CurrentTitle = $Host.UI.RawUI.WindowTitle
    $Host.UI.RawUI.WindowTitle =  "Loading Az ..."

    Set-Location -Path $ENV:USERPROFILE/code -ErrorAction stop

    $IsElevated = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
    if ($IsElevated) {
        Write-Host "$([char]9788) " -ForegroundColor Red -NoNewline
    }else {
        Write-Host "$([char]9788) " -ForegroundColor Yellow -NoNewline
    }
    Write-Host 'Craig ' -ForegroundColor Green -NoNewline
    Write-Host "[$((Get-Location).path)]" -NoNewline
    Write-Host
    Write-Output '# ' 

    $SubScriptionName = (Get-AzContext).Subscription.Name

    if ($SubScriptionName) {
        $WindowTitle = "PowerShell | Az - [$($SubscriptionName)]"
    } else {
        $WindowTitle = $CurrentTitle
    }

    $Host.UI.RawUI.WindowTitle =  $WindowTitle
 }

powershell 重装win10应用商店的方法

1,首先右键win10开始菜单找到windows PowerShell管理员命令,打开; <br/> 2,复制以下命令回车,等待几秒自动修复ok;

reinstall.ps
Get-AppXPackage *WindowsStore* -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}

powershell AWS cli

reading.ps1
aws ssm get-parameters-by-path --region [region] --path "[path]" --with-decryption --recursive 

aws lambda list-functions --region [aws region]] | convertfrom-json | select -ExpandProperty Functions |  select -ExpandProperty FunctionName
other_commands.ps1
# Create and update
aws ssm put-parameter --region [region] --name "[path]" --value "T00M4ny53cr3t5" --type "SecureString"
aws ssm put-parameter --region [region] --name "[path]" --value "53t3c45tr0n0my" --type "SecureString" --overwrite

# Read specific parameter
aws ssm get-parameters --region [region] --names "[path]/name" "[path]/password" --with-decryption

aws ssm get-parameter-history --region [region] --name "[path]/name" 

# Delete
aws ssm delete-parameters --region [region] --names "[path]"

ps_tools.ps1
Get-AWSCmdletName -Service "aws lambda" 

get-lmfunctionlist -region "ap-southeast-2" 

get-lmfunctionconfiguration -FunctionName "ascend-wavelength-api-bupa-b252-r252" -Region "ap-southeast-2"

powershell 用空格替换点

使用Powershell用文件名中的空格替换点

replaceDotsWithSpaces
gci -name | Rename-Item -NewName {$_ -replace '[()[\].]+',' '}

powershell 批量重命名

使用PowerShell批量重命名

bulkRename
Get-ChildItem -Filter '*current*' -Recurse | Rename-Item -NewName {$_.Name -replace 'current','new'}