In this article, we introduce the xtivdfreg command, which implements a general instrumental-variables (IV) approach for fitting panel-data models with many time-series observations, T, and unobserved common factors or interactive effects, as developed by Norkute et al. (2021, Journal of Econometrics 220: 416–446) and Cui et al. (2020a , ISER Discussion Paper 1101). The underlying idea of this approach is to project out the common factors from exogenous covariates using principal-components analysis and to run IV regression in both of two stages, using defactored covariates as instruments. The resulting two-stage IV estimator is valid for models with homogeneous or heterogeneous slope coefficients and has several advantages relative to existing popular approaches. In addition, the xtivdfreg command extends the two-stage IV approach in two major ways. First, the algorithm accommodates estimation of unbalanced panels. Second, the algorithm permits a flexible specification of instruments. We show that when one imposes zero factors, the xtivdfreg command can replicate the results of the popular Stata ivregress command. Notably, unlike ivregress, xtivdfreg permits estimation of the two-way error-components paneldata model with heterogeneous slope coefficients.