Windows Server
123 TopicsHow do I permanently store module?
Hi everyone, I'm new to powershell, I would like to know how to automatically load the modules I load with import-module in each instance; if I load the modules with import-module, when I close the instance I lose the module. I would like to know how to permanently load, for example, the KPI module in the $Env:PsModulePath variable. Obviously the question is answered to all the modules that I always want to keep in section even when I start the computer. I hope for an answerSolved116Views0likes4CommentsHow do I display a "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection" using PowerShell
PS C:\Users\serveradmin.QA> Get-ADDomainController -Filter * | Select-Object Name, OperationMasterRoles |Where-Object {$_.OperationMasterRoles} Name OperationMasterRoles ---- -------------------- ADAICTQA {SchemaMaster, DomainNamingMaster, PDCEmulator, RIDMaster...} in the above command I outputOperationMasterRoles Id like those to be displayed in the same row comma seperated ...how do I disect that ADPropertyValueCollection ?83Views0likes1CommentPowerShell data explanation and advice
Hi everyone. Not even sure how to ask and maybe it seems dramatic but I am reaching out for a little help here. Can someone help me understand this data I copied from PowerShell? I typed the same commands for user "smell" and user "Public". I have a node in network probably and I really hope for the worse to be honest. Reading about it got me pumped. Of course I have no idea if this could be the small window sun shines through or just another big nothing. Anyway, thanks to anyone who sets me straight about it. Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows PS C:\Users\smell> whoami / user ERROR: Invalid argument/option - '/'. Type "WHOAMI /?" for usage. PS C:\Users\smell> whoami /user USER INFORMATION ---------------- User Name SID =================== ============================================ thinkpadt16g2\smell S-1-5-21-2399413288-642862217-314349489-1001 PS C:\Users\smell> wmic useraccount where name='%username%' get domain,name,sid Node - THINKPADT16G2 ERROR: Description = Invalid query PS C:\Users\smell> wmic useraccount where name='%username%' get domain,name,sid Node - THINKPADT16G2 ERROR: Description = Invalid query PS C:\Users\smell> [Security.Principal.WindowsIdentity]::GetCurrent() | Select-Object -Property @('Name', 'User') Name User ---- ---- THINKPADT16G2\smell S-1-5-21-2399413288-642862217-314349489-1001 PS C:\Users\smell> [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value S-1-5-21-2399413288-642862217-314349489-1001 PS C:\Users\smell> wmic useraccount where name='smell' get sid Node - THINKPADT16G2 ERROR: Description = Invalid query PS C:\Users\smell> wmic useraccount where sid='<sid>' get domain,name Node - THINKPADT16G2 ERROR: Description = Invalid query PS C:\Users\smell> wmic useraccount where sid='S-1-5-21-2399413288-642862217-314349489-1001' get domain,name Unexpected switch at this level. PS C:\Users\smell> wmic useraccount get domain,name,sid Domain Name SID ThinkPadT16G2 Administrator S-1-5-21-2399413288-642862217-314349489-500 ThinkPadT16G2 DefaultAccount S-1-5-21-2399413288-642862217-314349489-503 ThinkPadT16G2 Guest S-1-5-21-2399413288-642862217-314349489-501 ThinkPadT16G2 smell S-1-5-21-2399413288-642862217-314349489-1001 ThinkPadT16G2 WDAGUtilityAccount S-1-5-21-2399413288-642862217-314349489-504 PS C:\Users\smell> Get-WmiObject win32_useraccount | Select domain,name,sid domain name sid ------ ---- --- ThinkPadT16G2 Administrator S-1-5-21-2399413288-642862217-314349489-500 ThinkPadT16G2 DefaultAccount S-1-5-21-2399413288-642862217-314349489-503 ThinkPadT16G2 Guest S-1-5-21-2399413288-642862217-314349489-501 ThinkPadT16G2 smell S-1-5-21-2399413288-642862217-314349489-1001 ThinkPadT16G2 WDAGUtilityAccount S-1-5-21-2399413288-642862217-314349489-504 PS C:\Users\smell> PS C:\Users\smell> Get-LocalUser | Select-Object -Property @('Name', 'SID') Name SID ---- --- Administrator S-1-5-21-2399413288-642862217-314349489-500 DefaultAccount S-1-5-21-2399413288-642862217-314349489-503 Guest S-1-5-21-2399413288-642862217-314349489-501 smell S-1-5-21-2399413288-642862217-314349489-1001 WDAGUtilityAccount S-1-5-21-2399413288-642862217-314349489-504 PS C:\Users\smell> Get-CimInstance -query 'Select * from win32_useraccount' | ft name, SID name SID ---- --- Administrator S-1-5-21-2399413288-642862217-314349489-500 DefaultAccount S-1-5-21-2399413288-642862217-314349489-503 Guest S-1-5-21-2399413288-642862217-314349489-501 smell S-1-5-21-2399413288-642862217-314349489-1001 WDAGUtilityAccount S-1-5-21-2399413288-642862217-314349489-504 PS C:\Users\smell> [Security.Principal.WindowsIdentity]::GetCurrent() | Select-Object -Property @('Name', 'User') Name User ---- ---- THINKPADT16G2\smell S-1-5-21-2399413288-642862217-314349489-1001 PS C:\Users\smell> C:\Users\Public C:\Users\Public : The term 'C:\Users\Public' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + C:\Users\Public + ~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Users\Public:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\smell> C:\Users\Public> C:\Users\Public> : The term 'C:\Users\Public>' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:2 + C:\Users\Public> + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Users\Public>:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\smell> C:\Users\ C:\Users\ : The term 'C:\Users\' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:2 + C:\Users\ + ~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Users\:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\smell> C:\Users C:\Users : The term 'C:\Users' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:2 + C:\Users + ~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Users:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\smell> PS C:\> Set-Location -PathC:\Users\Public Get-Process : A positional parameter cannot be found that accepts argument 'Set-Location'. At line:1 char:1 + PS C:\> Set-Location -PathC:\Users\Public + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand PS C:\Users\smell> Set-Location -Path C:\Users\Public PS C:\Users\Public> whoami /user USER INFORMATION ---------------- User Name SID =================== ============================================ thinkpadt16g2\smell S-1-5-21-2399413288-642862217-314349489-1001 PS C:\Users\Public> wmic useraccount where name='%username%' get domain,name,sid Node - THINKPADT16G2 ERROR: Description = Invalid query PS C:\Users\Public> [Security.Principal.WindowsIdentity]::GetCurrent() | Select-Object -Property @('Name', 'User') Name User ---- ---- THINKPADT16G2\smell S-1-5-21-2399413288-642862217-314349489-1001 PS C:\Users\Public> [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value S-1-5-21-2399413288-642862217-314349489-1001 PS C:\Users\Public> wmic useraccount where name='username' get sid Node - THINKPADT16G2 ERROR: Description = Invalid query PS C:\Users\Public> wmic useraccount where name='smell' get sid Node - THINKPADT16G2 ERROR: Description = Invalid query PS C:\Users\Public> wmic useraccount where sid='<sid>' get domain,name Node - THINKPADT16G2 ERROR: Description = Invalid query PS C:\Users\Public> wmic useraccount where sid='S-1-5-21-2399413288-642862217-314349489-1001' get domain,name Unexpected switch at this level. PS C:\Users\Public> wmic useraccount get domain,name,sid Domain Name SID ThinkPadT16G2 Administrator S-1-5-21-2399413288-642862217-314349489-500 ThinkPadT16G2 DefaultAccount S-1-5-21-2399413288-642862217-314349489-503 ThinkPadT16G2 Guest S-1-5-21-2399413288-642862217-314349489-501 ThinkPadT16G2 smell S-1-5-21-2399413288-642862217-314349489-1001 ThinkPadT16G2 WDAGUtilityAccount S-1-5-21-2399413288-642862217-314349489-504 PS C:\Users\Public> Get-WmiObject win32_useraccount | Select domain,name,sid domain name sid ------ ---- --- ThinkPadT16G2 Administrator S-1-5-21-2399413288-642862217-314349489-500 ThinkPadT16G2 DefaultAccount S-1-5-21-2399413288-642862217-314349489-503 ThinkPadT16G2 Guest S-1-5-21-2399413288-642862217-314349489-501 ThinkPadT16G2 smell S-1-5-21-2399413288-642862217-314349489-1001 ThinkPadT16G2 WDAGUtilityAccount S-1-5-21-2399413288-642862217-314349489-504 PS C:\Users\Public> Get-LocalUser | Select-Object -Property @('Name', 'SID') Name SID ---- --- Administrator S-1-5-21-2399413288-642862217-314349489-500 DefaultAccount S-1-5-21-2399413288-642862217-314349489-503 Guest S-1-5-21-2399413288-642862217-314349489-501 smell S-1-5-21-2399413288-642862217-314349489-1001 WDAGUtilityAccount S-1-5-21-2399413288-642862217-314349489-504 PS C:\Users\Public> Get-CimInstance -query 'Select * from win32_useraccount' | ft name, SID name SID ---- --- Administrator S-1-5-21-2399413288-642862217-314349489-500 DefaultAccount S-1-5-21-2399413288-642862217-314349489-503 Guest S-1-5-21-2399413288-642862217-314349489-501 smell S-1-5-21-2399413288-642862217-314349489-1001 WDAGUtilityAccount S-1-5-21-2399413288-642862217-314349489-504 PS C:\Users\Public>190Views0likes1CommentPowershell 7 - $time is not defined - Script from Microsoft
I get the error "Get-ADComputer: Variable: 'time' found in expression: $time is not defined.". The script is straight from Microsoft and $time is populated with a date/time. It seems very simple, I don't know what is going wrong. $DaysInactive = 90 $time = (Get-Date).Adddays(-($DaysInactive)) Get-ADComputer -searchbase "OU=SC,OU=Servers,DC=WSDOT,DC=LOC" -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties Name, OperatingSystem, SamAccountName, DistinguishedName (edited: correct code to reflect what was being tested)425Views0likes12Commentsshadowcopy
I see when I type wmic /? it shows me wmi options for shadowcopy. Thats great if using the deprecated WMI tool. Leads me to, so I can pull shadowcopy information using WMI tool, what about PS? When I search for shadowcopy with powershell, I find nothing. If there is some shadowcopy hooks in powershell what are they? And can we make them easier to find? seaching all PS docs using below comes up with nothing! Import-Module (Microsoft.PowerShell.Core) - PowerShell | Microsoft Learn\ Thanks! Rod170Views0likes2CommentsWeird Issue with "Enter"
So, I am running into this issue. It doesn't havppen in PowerShell but in PowerShell ISE. When I first open powershell ISE and type a command and hit enter, nothing happens. I hit enter a few more times, nothing happens. THen all of a sudden the command will run with a number of new prompts below it. It seems like the ISE is taking a pause. Anyone else run into this issue?492Views0likes6CommentsGet command status from Active Directory Domain machines
We need to get the status of few commands from a domain environment of around 400 machines. All may be windows 10 and windows 11 machines. Need to run this activity couple of times in a day. This may required few Power Shell commands with admin credentials OR we may run this from GPO. But I don't know where can we save the status result from machines (Power Shell script running is disabled via GPO so we may need to run PowerShell commands directly) Commands status result are required (windows 10/11): - Last Windows update status - Bit Locker installed and configured status - Total Local Admin users - LAPs is installed on not on machine208Views0likes0Commentsdate format getting changed from yyyy-mm-dd hh:mm:ss:fff to dd-mm-yyyy hh:mm
Hi All, I am trying to export data from sql server database to csv. data is getting export perfectly in csv file but date format is gettingfrom "yyyy-mm-dd hh:mm:ss:fff" to "dd-mm-yyyy hh:mm". please help to correct it Appreciate quick response. # Define database connection details $server = "YourServerName" $database = "YourDatabaseName" $username = "YourUsername" # Retrieve and decrypt the password $encryptedPassword = Get-Content "C:\path\to\encryptedPassword.txt" | ConvertTo-SecureString $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $encryptedPassword # SQL query $query = " SELECT * FROM YourTable " $data = Invoke-Sqlcmd -ServerInstance $server -Database $database -Credential $credentials -Query $query $data | Export-Csv -Path "C:\path\to\your\output.csv" -NoTypeInformation -Encoding UTF8 Write-Host "Data exported successfully to C:\path\to\your\output.csv"770Views0likes6CommentsPowershell help - export to CSV not working - exporting EXE version number - help required...
Hi, I am struggling to get this code below to output to a CSV file. I know I am doing something wrong. I need to get a version number from an EXE file on multiple devices. Thanks in advance all 🙂 Script Details : $ExportPath = "C:\Temp" $ComputerName = Get-Content “C:\Temp\DeviceList.txt” foreach ($Computer in $ComputerName){ $filePath = “\$computer\c$\Admin\program.exe” $fileVersion = (Get-Command $filePath).FileVersionInfo.FileVersion Write-Host “$computer The file version is $fileVersion” }913Views0likes6CommentsI need help with a Powershell Script I need to delete any Registry keys, values or Dwords
We updated our print server. I need to remove any Values, Friendly names, or DWORDs that reference the old print server. The old printers were pushed out via GPO by username, and we have computers with up to 50 copies of the same printer, I already tried editing the deployment scripts that did not remove them. I found a few scripts, but none of them seem to remove all of the entries Here is one of the scrips I am trying to edit $PrinterReg = Get-ChildItem -Path Registry::HKLM\SYSTEM\CurrentControlSet\Enum\SWD\PRINTENUM\* $PrinterName = "\\Starbase16\*" Foreach ($DeletePrinter in $PrinterReg){ $FriendlyName = $DeletePrinter.GetValue("Friendlyname") if($FriendlyName -eq $PrinterName){ Remove-Item -path $DeletePrinter.PSPath -Recurse Write-Host "Removing $friendlyname from $($DeletePrinter.PSPath)" }} Here are all the Keys that contain Starbase16 Computer\HKEY_CURRENT_USER\Printers\ConvertUserDevModesCount Computer\HKEY_CURRENT_USER\Printers\Settings Computer\HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client\Servers\Starbase16 Computer\HKEY_CURRENT_USER\Software\Xerox\PrinterDriver\V5.0\``Starbase16`2E-Main-Mailroom Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\PrinterMigrationEx\CSR|Starbase16 Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\Starbase16 Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\S-1-5-21-117609710-602162358-725345543-28548\Printers\Connections Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\Servers\Starbase16\Monitors\Client Side Port\ Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\Servers\Starbase16\Printers\528Views0likes3Comments