Posts (page 14)
- 6 min readTo print the previous month in PowerShell, you can use the Get-Date cmdlet to obtain the current date and then manipulate it to determine the previous month. You would typically subtract one month from the current date. Here's a simple example: $previousMonth = (Get-Date).AddMonths(-1).ToString("MMMM") Write-Output $previousMonth This script utilizes the AddMonths method, passing -1 as the argument to go back one month from the current date.
- 8 min readTo exclude the header of the Start-Transcript output in PowerShell, you can start the transcript but redirect the transcript file output to a new file, skipping the initial lines that contain the header information. First, initiate Start-Transcript to begin recording the session. Then, when you're done with the session or want to iterate over the content, stop the transcript with Stop-Transcript.
- 5 min readManaging the transition from one year to another in PowerShell can involve several considerations, especially if you are handling date-sensitive scripts or logs. Firstly, it might be important to ensure that any date comparisons or calculations account for the change in year. This can be accomplished using PowerShell's built-in date and time cmdlets, such as Get-Date, which can dynamically retrieve current dates and handle transitions smoothly.
- 10 min readTo retrieve the full Windows version using PowerShell, you can use the Get-ComputerInfo cmdlet which provides detailed information about the system, including the version of Windows. Specifically, you can extract the Windows version using the WindowsVersion, WindowsBuildLabEx, or WindowsBuildLab properties from the output of Get-ComputerInfo.
- 7 min readTo select an Excel range in PowerShell, you first need to automate Excel using the COM object model. Start by creating a new Excel application object and open the desired workbook and worksheet. You can then select a range using the Range property of the worksheet object. For example, if you want to select a specific range like "A1:C10", you can use $worksheet.Range("A1:C10"). To select this range, leverage the Select method on the specified range object.
- 6 min readTo add PowerShell commands to an npm script, you can define the desired command within the "scripts" section of your package.json file. When writing the script, you can combine PowerShell commands directly with Node.js-specific commands or other shell commands by utilizing the appropriate syntax. If you're using a Windows environment, these scripts will be executed with cmd.exe by default.
- 12 min readUsing PsExec and PowerShell to run commands on a remote computer involves a series of steps that leverage the capabilities of each tool to execute tasks remotely. PsExec is a command-line utility that allows you to execute processes on remote systems. It is part of the Sysinternals suite by Microsoft. To use PsExec, you generally need administrative access to the remote system. First, you download PsExec from the Sysinternals website and extract it.
- 6 min readTo create a checkbox automatically using PowerShell, you can use Windows Forms to design a simple graphical user interface (GUI). First, make sure to load the necessary assembly with [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms"). Then, create a form using New-Object System.Windows.Forms.Form. You can create a checkbox using New-Object System.Windows.Forms.CheckBox and configure its properties such as Text, Location, and Size.
- 8 min readIf you're looking to remove or change the present working directory in PowerShell, you can do so by navigating to a different directory or by closing the session. To change the directory, use the Set-Location cmdlet (or its alias cd) followed by the path you want to switch to, effectively leaving the current directory for another one. This doesn't delete the directory, but simply changes your current context to the specified location.
- 4 min readTo fetch logs content between two dates in Windows PowerShell, you can use the Get-EventLog cmdlet with the -After and -Before parameters. These parameters allow you to specify the start and end dates between which you want to fetch logs content. You can use the following syntax:Get-EventLog -LogName System -After "01/01/2021" -Before "01/31/2021"This command will fetch logs from the System log between January 1, 2021 and January 31, 2021.
- 5 min readTo restart a container instance using Powershell, you can use the Restart-Container cmdlet. First, you need to identify the specific container instance you want to restart by either the container name or ID. Then, you can run the following command:Restart-Container -Name [ContainerName]Replace [ContainerName] with the name of the container instance you want to restart. This command will gracefully stop and then start the container instance.
- 5 min readTo pass arguments to an MSI file using PowerShell, you can use the "Start-Process" cmdlet. This cmdlet allows you to execute a command and specify arguments to be passed to it.Here's an example of how you can pass arguments to an MSI file using PowerShell: Start-Process -FilePath "msiexec.exe" -ArgumentList "/i", "path\to\your\msi\file.