This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Get-Date -Format FileDate # 20180502 | |
| Get-Date -Format FileDateUniversal # 20180502Z | |
| Get-Date -Format FileDateTime # 20180502T1852552818 | |
| Get-Date -Format FileDateTimeUniversal # 20180502T2255061745Z | |
| Get-Date -Format f # Wednesday, May 02, 2018 6:51 PM | |
| Get-Date -Format F # Wednesday, May 02, 2018 6:51:03 PM | |
| Get-Date -Format g # 5/2/2018 6:51 PM | |
| Get-Date -Format G # 5/2/2018 6:51:03 PM | |
| Get-Date -Format s # 2018-05-02T18:51:03 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ##### | |
| # Require Module as part of a Script | |
| ##### | |
| # This examples checks for available PowerCLI module: VMware.VimAutomation.Core | |
| # Add "#REQUIRES" to the top of your .ps1 file | |
| # Use -Modules to specify a Module name | |
| # If the required modules are not in the current session, Windows PowerShell imports them. | |
| # If the modules cannot be imported, Windows PowerShell throws a terminating error. | |
| # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Remove the Curly Braces from a string such as after getting PSChildName. E.g. {636D9115-E54E-4673-B992-B51A8F8DDC8B} | |
| # Using trim : | |
| "{636D9115-E54E-4673-B992-B51A8F8DDC8B}".trim('{}') | |
| # Using multiple replace : | |
| "{636D9115-E54E-4673-B992-B51A8F8DDC8B}".Replace('{','').Replace('}','') | |
| # Using Replace with RegEx | |
| "{636D9115-E54E-4673-B992-B51A8F8DDC8B}" -replace '\{(.*)\}','$1' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $OS = ((Get-WmiObject Win32_OperatingSystem).Name).Split("|")[0] | |
| # Checking for Windows 7 or Windows 10 in Name | |
| If ($OS -contains "7" -or "10") {Write-Host "It's a Desktop!"} | |
| Else {Write-Host "It's a Server!"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # find out last boot time | |
| $os = Get-CimInstance -ClassName Win32_OperatingSystem | |
| $lastBoot = $os.LastBootUpTime | |
| # raw datetime output | |
| $lastBoot | |
| # e.g. \> Wednesday, December 5, 2018 2:31:48 AM | |
| # formatted string output | |
| Get-Date -Date $lastBoot -Format '"Last reboot at" MMM dd, yyyy "at" HH:mm:ss "and" fffff "Milliseconds."' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $timeout = New-TimeSpan -Seconds 30 | |
| $timer = [Diagnostics.Stopwatch]::new() # Create a timer | |
| $timerAndStart = [Diagnostics.Stopwatch]::StartNew() # Create a timer and start it | |
| $timer.Start() # Start the timer | |
| $timer.Stop() # Stop the timer | |
| $timer.Elapsed # Get time elapsed | |
| $timer.IsRunning # Is timer running? | |
| $timer.Restart() # restarts the time, doesn't stop | |
| $timer.Reset() # stops timer and sets to 0 | |
| $timer.Equals($timeout) # check if timer value equals a value such as timeout, returns boolean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| References: | |
| * http://powershell-guru.com/dont-13-use-write-host-properly-align-output/ | |
| #> | |
| # Solution 1 | |
| [PSCustomObject] @{ | |
| Date = Get-Date -Format d | |
| Computer = [System.Environment]::MachineName | |
| Username = ([Security.Principal.WindowsIdentity]::GetCurrent()).Name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| Resources: | |
| * https://blogs.technet.microsoft.com/dsheehan/2018/02/18/powershell-code-to-wait-for-a-background-process-to-finish-before-continuing-on-with-a-script/ | |
| #> | |
| # Check to see if the .NET Optimization Service is still running as it can block the Exchange initialization process. | |
| If (Get-Process mscorsvw -ErrorAction SilentlyContinue) { | |
| # It is so define the number of minutes the countdown timer should allow it to gracefully finish before giving up and exiting the script. | |
| $CountDownTimer = 10 | |
| Write-Host " " | |
| Write-Warning "The .NET Optimization Service is still running. Waiting up to $CountDownTimer minutes for it to gracefully shut down so the script can proceed." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Get the installed software from your Windows Operating Systems | |
| # From the WMI Object perspective | |
| Get-WmiObject -Class Win32_Product | Sort-Object Name | Select-Object Name,Vendor,Version,Caption | Format-Table -AutoSize | |
| # From the registry perspective | |
| Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Sort-Object DisplayName | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table -AutoSize |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # create secure string from plain-text string | |
| $secureString = ConvertTo-SecureString -AsPlainText -Force -String $password | |
| Write-Host "Secure string:",$secureString | |
| # convert secure string to encrypted string (for safe-ish storage to config/file/etc.) | |
| $encryptedString = ConvertFrom-SecureString -SecureString $secureString | |
| Write-Host "Encrypted string:",$encryptedString | |
| # convert encrypted string back to secure string | |
| $secureString = ConvertTo-SecureString -String $encryptedString |